mali_kbase_mem.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  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.c
  17. * Base kernel memory APIs
  18. */
  19. #ifdef CONFIG_DMA_SHARED_BUFFER
  20. #include <linux/dma-buf.h>
  21. #endif /* CONFIG_DMA_SHARED_BUFFER */
  22. #ifdef CONFIG_UMP
  23. #include <linux/ump.h>
  24. #endif /* CONFIG_UMP */
  25. #include <linux/kernel.h>
  26. #include <linux/bug.h>
  27. #include <linux/compat.h>
  28. #include <linux/sched/mm.h>
  29. #include <mali_kbase_config.h>
  30. #include <mali_kbase.h>
  31. #include <mali_midg_regmap.h>
  32. #include <mali_kbase_cache_policy.h>
  33. #include <mali_kbase_hw.h>
  34. #include <mali_kbase_hwaccess_time.h>
  35. #include <mali_kbase_tlstream.h>
  36. /**
  37. * @brief Check the zone compatibility of two regions.
  38. */
  39. static int kbase_region_tracker_match_zone(struct kbase_va_region *reg1,
  40. struct kbase_va_region *reg2)
  41. {
  42. return ((reg1->flags & KBASE_REG_ZONE_MASK) ==
  43. (reg2->flags & KBASE_REG_ZONE_MASK));
  44. }
  45. /* This function inserts a region into the tree. */
  46. static void kbase_region_tracker_insert(struct kbase_context *kctx, struct kbase_va_region *new_reg)
  47. {
  48. u64 start_pfn = new_reg->start_pfn;
  49. struct rb_node **link = &(kctx->reg_rbtree.rb_node);
  50. struct rb_node *parent = NULL;
  51. /* Find the right place in the tree using tree search */
  52. while (*link) {
  53. struct kbase_va_region *old_reg;
  54. parent = *link;
  55. old_reg = rb_entry(parent, struct kbase_va_region, rblink);
  56. /* RBTree requires no duplicate entries. */
  57. KBASE_DEBUG_ASSERT(old_reg->start_pfn != start_pfn);
  58. if (old_reg->start_pfn > start_pfn)
  59. link = &(*link)->rb_left;
  60. else
  61. link = &(*link)->rb_right;
  62. }
  63. /* Put the new node there, and rebalance tree */
  64. rb_link_node(&(new_reg->rblink), parent, link);
  65. rb_insert_color(&(new_reg->rblink), &(kctx->reg_rbtree));
  66. }
  67. /* Find allocated region enclosing free range. */
  68. static struct kbase_va_region *kbase_region_tracker_find_region_enclosing_range_free(
  69. struct kbase_context *kctx, u64 start_pfn, size_t nr_pages)
  70. {
  71. struct rb_node *rbnode;
  72. struct kbase_va_region *reg;
  73. u64 end_pfn = start_pfn + nr_pages;
  74. rbnode = kctx->reg_rbtree.rb_node;
  75. while (rbnode) {
  76. u64 tmp_start_pfn, tmp_end_pfn;
  77. reg = rb_entry(rbnode, struct kbase_va_region, rblink);
  78. tmp_start_pfn = reg->start_pfn;
  79. tmp_end_pfn = reg->start_pfn + reg->nr_pages;
  80. /* If start is lower than this, go left. */
  81. if (start_pfn < tmp_start_pfn)
  82. rbnode = rbnode->rb_left;
  83. /* If end is higher than this, then go right. */
  84. else if (end_pfn > tmp_end_pfn)
  85. rbnode = rbnode->rb_right;
  86. else /* Enclosing */
  87. return reg;
  88. }
  89. return NULL;
  90. }
  91. /* Find region enclosing given address. */
  92. struct kbase_va_region *kbase_region_tracker_find_region_enclosing_address(struct kbase_context *kctx, u64 gpu_addr)
  93. {
  94. struct rb_node *rbnode;
  95. struct kbase_va_region *reg;
  96. u64 gpu_pfn = gpu_addr >> PAGE_SHIFT;
  97. KBASE_DEBUG_ASSERT(kctx != NULL);
  98. lockdep_assert_held(&kctx->reg_lock);
  99. rbnode = kctx->reg_rbtree.rb_node;
  100. while (rbnode) {
  101. u64 tmp_start_pfn, tmp_end_pfn;
  102. reg = rb_entry(rbnode, struct kbase_va_region, rblink);
  103. tmp_start_pfn = reg->start_pfn;
  104. tmp_end_pfn = reg->start_pfn + reg->nr_pages;
  105. /* If start is lower than this, go left. */
  106. if (gpu_pfn < tmp_start_pfn)
  107. rbnode = rbnode->rb_left;
  108. /* If end is higher than this, then go right. */
  109. else if (gpu_pfn >= tmp_end_pfn)
  110. rbnode = rbnode->rb_right;
  111. else /* Enclosing */
  112. return reg;
  113. }
  114. return NULL;
  115. }
  116. /* Find region with given base address */
  117. struct kbase_va_region *kbase_region_tracker_find_region_base_address(struct kbase_context *kctx, u64 gpu_addr)
  118. {
  119. u64 gpu_pfn = gpu_addr >> PAGE_SHIFT;
  120. struct rb_node *rbnode;
  121. struct kbase_va_region *reg;
  122. KBASE_DEBUG_ASSERT(kctx != NULL);
  123. lockdep_assert_held(&kctx->reg_lock);
  124. rbnode = kctx->reg_rbtree.rb_node;
  125. while (rbnode) {
  126. reg = rb_entry(rbnode, struct kbase_va_region, rblink);
  127. if (reg->start_pfn > gpu_pfn)
  128. rbnode = rbnode->rb_left;
  129. else if (reg->start_pfn < gpu_pfn)
  130. rbnode = rbnode->rb_right;
  131. else
  132. return reg;
  133. }
  134. return NULL;
  135. }
  136. /* Find region meeting given requirements */
  137. static struct kbase_va_region *kbase_region_tracker_find_region_meeting_reqs(struct kbase_context *kctx, struct kbase_va_region *reg_reqs, size_t nr_pages, size_t align)
  138. {
  139. struct rb_node *rbnode;
  140. struct kbase_va_region *reg;
  141. /* Note that this search is a linear search, as we do not have a target
  142. address in mind, so does not benefit from the rbtree search */
  143. rbnode = rb_first(&(kctx->reg_rbtree));
  144. while (rbnode) {
  145. reg = rb_entry(rbnode, struct kbase_va_region, rblink);
  146. if ((reg->nr_pages >= nr_pages) &&
  147. (reg->flags & KBASE_REG_FREE) &&
  148. kbase_region_tracker_match_zone(reg, reg_reqs)) {
  149. /* Check alignment */
  150. u64 start_pfn = (reg->start_pfn + align - 1) & ~(align - 1);
  151. if ((start_pfn >= reg->start_pfn) &&
  152. (start_pfn <= (reg->start_pfn + reg->nr_pages - 1)) &&
  153. ((start_pfn + nr_pages - 1) <= (reg->start_pfn + reg->nr_pages - 1)))
  154. return reg;
  155. }
  156. rbnode = rb_next(rbnode);
  157. }
  158. return NULL;
  159. }
  160. /**
  161. * @brief Remove a region object from the global list.
  162. *
  163. * The region reg is removed, possibly by merging with other free and
  164. * compatible adjacent regions. It must be called with the context
  165. * region lock held. The associated memory is not released (see
  166. * kbase_free_alloced_region). Internal use only.
  167. */
  168. static int kbase_remove_va_region(struct kbase_context *kctx, struct kbase_va_region *reg)
  169. {
  170. struct rb_node *rbprev;
  171. struct kbase_va_region *prev = NULL;
  172. struct rb_node *rbnext;
  173. struct kbase_va_region *next = NULL;
  174. int merged_front = 0;
  175. int merged_back = 0;
  176. int err = 0;
  177. /* Try to merge with the previous block first */
  178. rbprev = rb_prev(&(reg->rblink));
  179. if (rbprev) {
  180. prev = rb_entry(rbprev, struct kbase_va_region, rblink);
  181. if ((prev->flags & KBASE_REG_FREE) && kbase_region_tracker_match_zone(prev, reg)) {
  182. /* We're compatible with the previous VMA, merge with it */
  183. prev->nr_pages += reg->nr_pages;
  184. rb_erase(&(reg->rblink), &kctx->reg_rbtree);
  185. reg = prev;
  186. merged_front = 1;
  187. }
  188. }
  189. /* Try to merge with the next block second */
  190. /* Note we do the lookup here as the tree may have been rebalanced. */
  191. rbnext = rb_next(&(reg->rblink));
  192. if (rbnext) {
  193. /* We're compatible with the next VMA, merge with it */
  194. next = rb_entry(rbnext, struct kbase_va_region, rblink);
  195. if ((next->flags & KBASE_REG_FREE) && kbase_region_tracker_match_zone(next, reg)) {
  196. next->start_pfn = reg->start_pfn;
  197. next->nr_pages += reg->nr_pages;
  198. rb_erase(&(reg->rblink), &kctx->reg_rbtree);
  199. merged_back = 1;
  200. if (merged_front) {
  201. /* We already merged with prev, free it */
  202. kbase_free_alloced_region(reg);
  203. }
  204. }
  205. }
  206. /* If we failed to merge then we need to add a new block */
  207. if (!(merged_front || merged_back)) {
  208. /*
  209. * We didn't merge anything. Add a new free
  210. * placeholder and remove the original one.
  211. */
  212. struct kbase_va_region *free_reg;
  213. free_reg = kbase_alloc_free_region(kctx, reg->start_pfn, reg->nr_pages, reg->flags & KBASE_REG_ZONE_MASK);
  214. if (!free_reg) {
  215. err = -ENOMEM;
  216. goto out;
  217. }
  218. rb_replace_node(&(reg->rblink), &(free_reg->rblink), &(kctx->reg_rbtree));
  219. }
  220. out:
  221. return err;
  222. }
  223. /**
  224. * @brief Insert a VA region to the list, replacing the current at_reg.
  225. */
  226. static int kbase_insert_va_region_nolock(struct kbase_context *kctx, struct kbase_va_region *new_reg, struct kbase_va_region *at_reg, u64 start_pfn, size_t nr_pages)
  227. {
  228. int err = 0;
  229. /* Must be a free region */
  230. KBASE_DEBUG_ASSERT((at_reg->flags & KBASE_REG_FREE) != 0);
  231. /* start_pfn should be contained within at_reg */
  232. KBASE_DEBUG_ASSERT((start_pfn >= at_reg->start_pfn) && (start_pfn < at_reg->start_pfn + at_reg->nr_pages));
  233. /* at least nr_pages from start_pfn should be contained within at_reg */
  234. KBASE_DEBUG_ASSERT(start_pfn + nr_pages <= at_reg->start_pfn + at_reg->nr_pages);
  235. new_reg->start_pfn = start_pfn;
  236. new_reg->nr_pages = nr_pages;
  237. /* Regions are a whole use, so swap and delete old one. */
  238. if (at_reg->start_pfn == start_pfn && at_reg->nr_pages == nr_pages) {
  239. rb_replace_node(&(at_reg->rblink), &(new_reg->rblink), &(kctx->reg_rbtree));
  240. kbase_free_alloced_region(at_reg);
  241. }
  242. /* New region replaces the start of the old one, so insert before. */
  243. else if (at_reg->start_pfn == start_pfn) {
  244. at_reg->start_pfn += nr_pages;
  245. KBASE_DEBUG_ASSERT(at_reg->nr_pages >= nr_pages);
  246. at_reg->nr_pages -= nr_pages;
  247. kbase_region_tracker_insert(kctx, new_reg);
  248. }
  249. /* New region replaces the end of the old one, so insert after. */
  250. else if ((at_reg->start_pfn + at_reg->nr_pages) == (start_pfn + nr_pages)) {
  251. at_reg->nr_pages -= nr_pages;
  252. kbase_region_tracker_insert(kctx, new_reg);
  253. }
  254. /* New region splits the old one, so insert and create new */
  255. else {
  256. struct kbase_va_region *new_front_reg;
  257. new_front_reg = kbase_alloc_free_region(kctx,
  258. at_reg->start_pfn,
  259. start_pfn - at_reg->start_pfn,
  260. at_reg->flags & KBASE_REG_ZONE_MASK);
  261. if (new_front_reg) {
  262. at_reg->nr_pages -= nr_pages + new_front_reg->nr_pages;
  263. at_reg->start_pfn = start_pfn + nr_pages;
  264. kbase_region_tracker_insert(kctx, new_front_reg);
  265. kbase_region_tracker_insert(kctx, new_reg);
  266. } else {
  267. err = -ENOMEM;
  268. }
  269. }
  270. return err;
  271. }
  272. /**
  273. * @brief Add a VA region to the list.
  274. */
  275. int kbase_add_va_region(struct kbase_context *kctx,
  276. struct kbase_va_region *reg, u64 addr,
  277. size_t nr_pages, size_t align)
  278. {
  279. struct kbase_va_region *tmp;
  280. u64 gpu_pfn = addr >> PAGE_SHIFT;
  281. int err = 0;
  282. KBASE_DEBUG_ASSERT(kctx != NULL);
  283. KBASE_DEBUG_ASSERT(reg != NULL);
  284. lockdep_assert_held(&kctx->reg_lock);
  285. if (!align)
  286. align = 1;
  287. /* must be a power of 2 */
  288. KBASE_DEBUG_ASSERT((align & (align - 1)) == 0);
  289. KBASE_DEBUG_ASSERT(nr_pages > 0);
  290. /* Path 1: Map a specific address. Find the enclosing region, which *must* be free. */
  291. if (gpu_pfn) {
  292. struct device *dev = kctx->kbdev->dev;
  293. KBASE_DEBUG_ASSERT(!(gpu_pfn & (align - 1)));
  294. tmp = kbase_region_tracker_find_region_enclosing_range_free(kctx, gpu_pfn, nr_pages);
  295. if (!tmp) {
  296. dev_warn(dev, "Enclosing region not found: 0x%08llx gpu_pfn, %zu nr_pages", gpu_pfn, nr_pages);
  297. err = -ENOMEM;
  298. goto exit;
  299. }
  300. if ((!kbase_region_tracker_match_zone(tmp, reg)) ||
  301. (!(tmp->flags & KBASE_REG_FREE))) {
  302. dev_warn(dev, "Zone mismatch: %lu != %lu", tmp->flags & KBASE_REG_ZONE_MASK, reg->flags & KBASE_REG_ZONE_MASK);
  303. dev_warn(dev, "!(tmp->flags & KBASE_REG_FREE): tmp->start_pfn=0x%llx tmp->flags=0x%lx tmp->nr_pages=0x%zx gpu_pfn=0x%llx nr_pages=0x%zx\n", tmp->start_pfn, tmp->flags, tmp->nr_pages, gpu_pfn, nr_pages);
  304. dev_warn(dev, "in function %s (%p, %p, 0x%llx, 0x%zx, 0x%zx)\n", __func__, kctx, reg, addr, nr_pages, align);
  305. err = -ENOMEM;
  306. goto exit;
  307. }
  308. err = kbase_insert_va_region_nolock(kctx, reg, tmp, gpu_pfn, nr_pages);
  309. if (err) {
  310. dev_warn(dev, "Failed to insert va region");
  311. err = -ENOMEM;
  312. goto exit;
  313. }
  314. goto exit;
  315. }
  316. /* Path 2: Map any free address which meets the requirements. */
  317. {
  318. u64 start_pfn;
  319. /*
  320. * Depending on the zone the allocation request is for
  321. * we might need to retry it.
  322. */
  323. do {
  324. tmp = kbase_region_tracker_find_region_meeting_reqs(
  325. kctx, reg, nr_pages, align);
  326. if (tmp) {
  327. start_pfn = (tmp->start_pfn + align - 1) &
  328. ~(align - 1);
  329. err = kbase_insert_va_region_nolock(kctx, reg,
  330. tmp, start_pfn, nr_pages);
  331. break;
  332. }
  333. /*
  334. * If the allocation is not from the same zone as JIT
  335. * then don't retry, we're out of VA and there is
  336. * nothing which can be done about it.
  337. */
  338. if ((reg->flags & KBASE_REG_ZONE_MASK) !=
  339. KBASE_REG_ZONE_CUSTOM_VA)
  340. break;
  341. } while (kbase_jit_evict(kctx));
  342. if (!tmp)
  343. err = -ENOMEM;
  344. }
  345. exit:
  346. return err;
  347. }
  348. /**
  349. * @brief Initialize the internal region tracker data structure.
  350. */
  351. static void kbase_region_tracker_ds_init(struct kbase_context *kctx,
  352. struct kbase_va_region *same_va_reg,
  353. struct kbase_va_region *exec_reg,
  354. struct kbase_va_region *custom_va_reg)
  355. {
  356. kctx->reg_rbtree = RB_ROOT;
  357. kbase_region_tracker_insert(kctx, same_va_reg);
  358. /* exec and custom_va_reg doesn't always exist */
  359. if (exec_reg && custom_va_reg) {
  360. kbase_region_tracker_insert(kctx, exec_reg);
  361. kbase_region_tracker_insert(kctx, custom_va_reg);
  362. }
  363. }
  364. void kbase_region_tracker_term(struct kbase_context *kctx)
  365. {
  366. struct rb_node *rbnode;
  367. struct kbase_va_region *reg;
  368. do {
  369. rbnode = rb_first(&(kctx->reg_rbtree));
  370. if (rbnode) {
  371. rb_erase(rbnode, &(kctx->reg_rbtree));
  372. reg = rb_entry(rbnode, struct kbase_va_region, rblink);
  373. kbase_free_alloced_region(reg);
  374. }
  375. } while (rbnode);
  376. }
  377. /**
  378. * Initialize the region tracker data structure.
  379. */
  380. int kbase_region_tracker_init(struct kbase_context *kctx)
  381. {
  382. struct kbase_va_region *same_va_reg;
  383. struct kbase_va_region *exec_reg = NULL;
  384. struct kbase_va_region *custom_va_reg = NULL;
  385. size_t same_va_bits = sizeof(void *) * BITS_PER_BYTE;
  386. u64 custom_va_size = KBASE_REG_ZONE_CUSTOM_VA_SIZE;
  387. u64 gpu_va_limit = (1ULL << kctx->kbdev->gpu_props.mmu.va_bits) >> PAGE_SHIFT;
  388. u64 same_va_pages;
  389. int err;
  390. /* Take the lock as kbase_free_alloced_region requires it */
  391. kbase_gpu_vm_lock(kctx);
  392. #if defined(CONFIG_ARM64)
  393. same_va_bits = VA_BITS;
  394. #elif defined(CONFIG_X86_64)
  395. same_va_bits = 47;
  396. #elif defined(CONFIG_64BIT)
  397. #error Unsupported 64-bit architecture
  398. #endif
  399. #ifdef CONFIG_64BIT
  400. if (kctx->is_compat)
  401. same_va_bits = 32;
  402. else if (kbase_hw_has_feature(kctx->kbdev, BASE_HW_FEATURE_33BIT_VA))
  403. same_va_bits = 33;
  404. #endif
  405. if (kctx->kbdev->gpu_props.mmu.va_bits < same_va_bits) {
  406. err = -EINVAL;
  407. goto fail_unlock;
  408. }
  409. same_va_pages = (1ULL << (same_va_bits - PAGE_SHIFT)) - 1;
  410. /* all have SAME_VA */
  411. same_va_reg = kbase_alloc_free_region(kctx, 1,
  412. same_va_pages,
  413. KBASE_REG_ZONE_SAME_VA);
  414. if (!same_va_reg) {
  415. err = -ENOMEM;
  416. goto fail_unlock;
  417. }
  418. #ifdef CONFIG_64BIT
  419. /* 32-bit clients have exec and custom VA zones */
  420. if (kctx->is_compat) {
  421. #endif
  422. if (gpu_va_limit <= KBASE_REG_ZONE_CUSTOM_VA_BASE) {
  423. err = -EINVAL;
  424. goto fail_free_same_va;
  425. }
  426. /* If the current size of TMEM is out of range of the
  427. * virtual address space addressable by the MMU then
  428. * we should shrink it to fit
  429. */
  430. if ((KBASE_REG_ZONE_CUSTOM_VA_BASE + KBASE_REG_ZONE_CUSTOM_VA_SIZE) >= gpu_va_limit)
  431. custom_va_size = gpu_va_limit - KBASE_REG_ZONE_CUSTOM_VA_BASE;
  432. exec_reg = kbase_alloc_free_region(kctx,
  433. KBASE_REG_ZONE_EXEC_BASE,
  434. KBASE_REG_ZONE_EXEC_SIZE,
  435. KBASE_REG_ZONE_EXEC);
  436. if (!exec_reg) {
  437. err = -ENOMEM;
  438. goto fail_free_same_va;
  439. }
  440. custom_va_reg = kbase_alloc_free_region(kctx,
  441. KBASE_REG_ZONE_CUSTOM_VA_BASE,
  442. custom_va_size, KBASE_REG_ZONE_CUSTOM_VA);
  443. if (!custom_va_reg) {
  444. err = -ENOMEM;
  445. goto fail_free_exec;
  446. }
  447. #ifdef CONFIG_64BIT
  448. }
  449. #endif
  450. kbase_region_tracker_ds_init(kctx, same_va_reg, exec_reg, custom_va_reg);
  451. kctx->same_va_end = same_va_pages + 1;
  452. kbase_gpu_vm_unlock(kctx);
  453. return 0;
  454. fail_free_exec:
  455. kbase_free_alloced_region(exec_reg);
  456. fail_free_same_va:
  457. kbase_free_alloced_region(same_va_reg);
  458. fail_unlock:
  459. kbase_gpu_vm_unlock(kctx);
  460. return err;
  461. }
  462. int kbase_region_tracker_init_jit(struct kbase_context *kctx, u64 jit_va_pages)
  463. {
  464. #ifdef CONFIG_64BIT
  465. struct kbase_va_region *same_va;
  466. struct kbase_va_region *custom_va_reg;
  467. u64 same_va_bits;
  468. u64 total_va_size;
  469. int err;
  470. /*
  471. * Nothing to do for 32-bit clients, JIT uses the existing
  472. * custom VA zone.
  473. */
  474. if (kctx->is_compat)
  475. return 0;
  476. #if defined(CONFIG_ARM64)
  477. same_va_bits = VA_BITS;
  478. #elif defined(CONFIG_X86_64)
  479. same_va_bits = 47;
  480. #elif defined(CONFIG_64BIT)
  481. #error Unsupported 64-bit architecture
  482. #endif
  483. if (kbase_hw_has_feature(kctx->kbdev, BASE_HW_FEATURE_33BIT_VA))
  484. same_va_bits = 33;
  485. total_va_size = (1ULL << (same_va_bits - PAGE_SHIFT)) - 1;
  486. kbase_gpu_vm_lock(kctx);
  487. /*
  488. * Modify the same VA free region after creation. Be careful to ensure
  489. * that allocations haven't been made as they could cause an overlap
  490. * to happen with existing same VA allocations and the custom VA zone.
  491. */
  492. same_va = kbase_region_tracker_find_region_base_address(kctx,
  493. PAGE_SIZE);
  494. if (!same_va) {
  495. err = -ENOMEM;
  496. goto fail_unlock;
  497. }
  498. /* The region flag or region size has changed since creation so bail. */
  499. if ((!(same_va->flags & KBASE_REG_FREE)) ||
  500. (same_va->nr_pages != total_va_size)) {
  501. err = -ENOMEM;
  502. goto fail_unlock;
  503. }
  504. /* It's safe to adjust the same VA zone now */
  505. same_va->nr_pages -= jit_va_pages;
  506. kctx->same_va_end -= jit_va_pages;
  507. /*
  508. * Create a custom VA zone at the end of the VA for allocations which
  509. * JIT can use so it doesn't have to allocate VA from the kernel.
  510. */
  511. custom_va_reg = kbase_alloc_free_region(kctx,
  512. kctx->same_va_end,
  513. jit_va_pages,
  514. KBASE_REG_ZONE_CUSTOM_VA);
  515. if (!custom_va_reg) {
  516. /*
  517. * The context will be destroyed if we fail here so no point
  518. * reverting the change we made to same_va.
  519. */
  520. err = -ENOMEM;
  521. goto fail_unlock;
  522. }
  523. kbase_region_tracker_insert(kctx, custom_va_reg);
  524. kbase_gpu_vm_unlock(kctx);
  525. return 0;
  526. fail_unlock:
  527. kbase_gpu_vm_unlock(kctx);
  528. return err;
  529. #else
  530. return 0;
  531. #endif
  532. }
  533. int kbase_mem_init(struct kbase_device *kbdev)
  534. {
  535. struct kbasep_mem_device *memdev;
  536. KBASE_DEBUG_ASSERT(kbdev);
  537. memdev = &kbdev->memdev;
  538. kbdev->mem_pool_max_size_default = KBASE_MEM_POOL_MAX_SIZE_KCTX;
  539. /* Initialize memory usage */
  540. atomic_set(&memdev->used_pages, 0);
  541. return kbase_mem_pool_init(&kbdev->mem_pool,
  542. KBASE_MEM_POOL_MAX_SIZE_KBDEV, kbdev, NULL);
  543. }
  544. void kbase_mem_halt(struct kbase_device *kbdev)
  545. {
  546. CSTD_UNUSED(kbdev);
  547. }
  548. void kbase_mem_term(struct kbase_device *kbdev)
  549. {
  550. struct kbasep_mem_device *memdev;
  551. int pages;
  552. KBASE_DEBUG_ASSERT(kbdev);
  553. memdev = &kbdev->memdev;
  554. pages = atomic_read(&memdev->used_pages);
  555. if (pages != 0)
  556. dev_warn(kbdev->dev, "%s: %d pages in use!\n", __func__, pages);
  557. kbase_mem_pool_term(&kbdev->mem_pool);
  558. }
  559. /**
  560. * @brief Allocate a free region object.
  561. *
  562. * The allocated object is not part of any list yet, and is flagged as
  563. * KBASE_REG_FREE. No mapping is allocated yet.
  564. *
  565. * zone is KBASE_REG_ZONE_CUSTOM_VA, KBASE_REG_ZONE_SAME_VA, or KBASE_REG_ZONE_EXEC
  566. *
  567. */
  568. struct kbase_va_region *kbase_alloc_free_region(struct kbase_context *kctx, u64 start_pfn, size_t nr_pages, int zone)
  569. {
  570. struct kbase_va_region *new_reg;
  571. KBASE_DEBUG_ASSERT(kctx != NULL);
  572. /* zone argument should only contain zone related region flags */
  573. KBASE_DEBUG_ASSERT((zone & ~KBASE_REG_ZONE_MASK) == 0);
  574. KBASE_DEBUG_ASSERT(nr_pages > 0);
  575. /* 64-bit address range is the max */
  576. KBASE_DEBUG_ASSERT(start_pfn + nr_pages <= (U64_MAX / PAGE_SIZE));
  577. new_reg = kzalloc(sizeof(*new_reg), GFP_KERNEL);
  578. if (!new_reg)
  579. return NULL;
  580. new_reg->cpu_alloc = NULL; /* no alloc bound yet */
  581. new_reg->gpu_alloc = NULL; /* no alloc bound yet */
  582. new_reg->kctx = kctx;
  583. new_reg->flags = zone | KBASE_REG_FREE;
  584. new_reg->flags |= KBASE_REG_GROWABLE;
  585. new_reg->start_pfn = start_pfn;
  586. new_reg->nr_pages = nr_pages;
  587. return new_reg;
  588. }
  589. /**
  590. * @brief Free a region object.
  591. *
  592. * The described region must be freed of any mapping.
  593. *
  594. * If the region is not flagged as KBASE_REG_FREE, the region's
  595. * alloc object will be released.
  596. * It is a bug if no alloc object exists for non-free regions.
  597. *
  598. */
  599. void kbase_free_alloced_region(struct kbase_va_region *reg)
  600. {
  601. if (!(reg->flags & KBASE_REG_FREE)) {
  602. /*
  603. * The physical allocation should have been removed from the
  604. * eviction list before this function is called. However, in the
  605. * case of abnormal process termination or the app leaking the
  606. * memory kbase_mem_free_region is not called so it can still be
  607. * on the list at termination time of the region tracker.
  608. */
  609. if (!list_empty(&reg->gpu_alloc->evict_node)) {
  610. /*
  611. * Unlink the physical allocation before unmaking it
  612. * evictable so that the allocation isn't grown back to
  613. * its last backed size as we're going to unmap it
  614. * anyway.
  615. */
  616. reg->cpu_alloc->reg = NULL;
  617. if (reg->cpu_alloc != reg->gpu_alloc)
  618. reg->gpu_alloc->reg = NULL;
  619. /*
  620. * If a region has been made evictable then we must
  621. * unmake it before trying to free it.
  622. * If the memory hasn't been reclaimed it will be
  623. * unmapped and freed below, if it has been reclaimed
  624. * then the operations below are no-ops.
  625. */
  626. if (reg->flags & KBASE_REG_DONT_NEED) {
  627. KBASE_DEBUG_ASSERT(reg->cpu_alloc->type ==
  628. KBASE_MEM_TYPE_NATIVE);
  629. kbase_mem_evictable_unmake(reg->gpu_alloc);
  630. }
  631. }
  632. /*
  633. * Remove the region from the sticky resource metadata
  634. * list should it be there.
  635. */
  636. kbase_sticky_resource_release(reg->kctx, NULL,
  637. reg->start_pfn << PAGE_SHIFT);
  638. kbase_mem_phy_alloc_put(reg->cpu_alloc);
  639. kbase_mem_phy_alloc_put(reg->gpu_alloc);
  640. /* To detect use-after-free in debug builds */
  641. KBASE_DEBUG_CODE(reg->flags |= KBASE_REG_FREE);
  642. }
  643. kfree(reg);
  644. }
  645. void kbase_mmu_update(struct kbase_context *kctx)
  646. {
  647. KBASE_DEBUG_ASSERT(kctx != NULL);
  648. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  649. /* ASSERT that the context has a valid as_nr, which is only the case
  650. * when it's scheduled in.
  651. *
  652. * as_nr won't change because the caller has the runpool_irq lock */
  653. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  654. lockdep_assert_held(&kctx->kbdev->as[kctx->as_nr].transaction_mutex);
  655. kctx->kbdev->mmu_mode->update(kctx);
  656. }
  657. void kbase_mmu_disable(struct kbase_context *kctx)
  658. {
  659. KBASE_DEBUG_ASSERT(kctx != NULL);
  660. /* ASSERT that the context has a valid as_nr, which is only the case
  661. * when it's scheduled in.
  662. *
  663. * as_nr won't change because the caller has the runpool_irq lock */
  664. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  665. kctx->kbdev->mmu_mode->disable_as(kctx->kbdev, kctx->as_nr);
  666. }
  667. void kbase_mmu_disable_as(struct kbase_device *kbdev, int as_nr)
  668. {
  669. kbdev->mmu_mode->disable_as(kbdev, as_nr);
  670. }
  671. int kbase_gpu_mmap(struct kbase_context *kctx, struct kbase_va_region *reg, u64 addr, size_t nr_pages, size_t align)
  672. {
  673. int err;
  674. size_t i = 0;
  675. unsigned long attr;
  676. unsigned long mask = ~KBASE_REG_MEMATTR_MASK;
  677. if ((kctx->kbdev->system_coherency == COHERENCY_ACE) &&
  678. (reg->flags & KBASE_REG_SHARE_BOTH))
  679. attr = KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_OUTER_WA);
  680. else
  681. attr = KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_WRITE_ALLOC);
  682. KBASE_DEBUG_ASSERT(kctx != NULL);
  683. KBASE_DEBUG_ASSERT(reg != NULL);
  684. err = kbase_add_va_region(kctx, reg, addr, nr_pages, align);
  685. if (err)
  686. return err;
  687. if (reg->gpu_alloc->type == KBASE_MEM_TYPE_ALIAS) {
  688. u64 stride;
  689. struct kbase_mem_phy_alloc *alloc;
  690. alloc = reg->gpu_alloc;
  691. stride = alloc->imported.alias.stride;
  692. KBASE_DEBUG_ASSERT(alloc->imported.alias.aliased);
  693. for (i = 0; i < alloc->imported.alias.nents; i++) {
  694. if (alloc->imported.alias.aliased[i].alloc) {
  695. err = kbase_mmu_insert_pages(kctx,
  696. reg->start_pfn + (i * stride),
  697. alloc->imported.alias.aliased[i].alloc->pages + alloc->imported.alias.aliased[i].offset,
  698. alloc->imported.alias.aliased[i].length,
  699. reg->flags);
  700. if (err)
  701. goto bad_insert;
  702. kbase_mem_phy_alloc_gpu_mapped(alloc->imported.alias.aliased[i].alloc);
  703. } else {
  704. err = kbase_mmu_insert_single_page(kctx,
  705. reg->start_pfn + i * stride,
  706. page_to_phys(kctx->aliasing_sink_page),
  707. alloc->imported.alias.aliased[i].length,
  708. (reg->flags & mask) | attr);
  709. if (err)
  710. goto bad_insert;
  711. }
  712. }
  713. } else {
  714. err = kbase_mmu_insert_pages(kctx, reg->start_pfn,
  715. kbase_get_gpu_phy_pages(reg),
  716. kbase_reg_current_backed_size(reg),
  717. reg->flags);
  718. if (err)
  719. goto bad_insert;
  720. kbase_mem_phy_alloc_gpu_mapped(reg->gpu_alloc);
  721. }
  722. return err;
  723. bad_insert:
  724. if (reg->gpu_alloc->type == KBASE_MEM_TYPE_ALIAS) {
  725. u64 stride;
  726. stride = reg->gpu_alloc->imported.alias.stride;
  727. KBASE_DEBUG_ASSERT(reg->gpu_alloc->imported.alias.aliased);
  728. while (i--)
  729. if (reg->gpu_alloc->imported.alias.aliased[i].alloc) {
  730. kbase_mmu_teardown_pages(kctx, reg->start_pfn + (i * stride), reg->gpu_alloc->imported.alias.aliased[i].length);
  731. kbase_mem_phy_alloc_gpu_unmapped(reg->gpu_alloc->imported.alias.aliased[i].alloc);
  732. }
  733. }
  734. kbase_remove_va_region(kctx, reg);
  735. return err;
  736. }
  737. int kbase_gpu_munmap(struct kbase_context *kctx, struct kbase_va_region *reg)
  738. {
  739. int err;
  740. if (reg->start_pfn == 0)
  741. return 0;
  742. if (reg->gpu_alloc && reg->gpu_alloc->type == KBASE_MEM_TYPE_ALIAS) {
  743. size_t i;
  744. err = kbase_mmu_teardown_pages(kctx, reg->start_pfn, reg->nr_pages);
  745. KBASE_DEBUG_ASSERT(reg->gpu_alloc->imported.alias.aliased);
  746. for (i = 0; i < reg->gpu_alloc->imported.alias.nents; i++)
  747. if (reg->gpu_alloc->imported.alias.aliased[i].alloc)
  748. kbase_mem_phy_alloc_gpu_unmapped(reg->gpu_alloc->imported.alias.aliased[i].alloc);
  749. } else {
  750. err = kbase_mmu_teardown_pages(kctx, reg->start_pfn, kbase_reg_current_backed_size(reg));
  751. kbase_mem_phy_alloc_gpu_unmapped(reg->gpu_alloc);
  752. }
  753. if (err)
  754. return err;
  755. err = kbase_remove_va_region(kctx, reg);
  756. return err;
  757. }
  758. static struct kbase_cpu_mapping *kbasep_find_enclosing_cpu_mapping_of_region(const struct kbase_va_region *reg, unsigned long uaddr, size_t size)
  759. {
  760. struct kbase_cpu_mapping *map;
  761. struct list_head *pos;
  762. KBASE_DEBUG_ASSERT(reg != NULL);
  763. KBASE_DEBUG_ASSERT(reg->cpu_alloc);
  764. if ((uintptr_t) uaddr + size < (uintptr_t) uaddr) /* overflow check */
  765. return NULL;
  766. list_for_each(pos, &reg->cpu_alloc->mappings) {
  767. map = list_entry(pos, struct kbase_cpu_mapping, mappings_list);
  768. if (map->vm_start <= uaddr && map->vm_end >= uaddr + size)
  769. return map;
  770. }
  771. return NULL;
  772. }
  773. int kbasep_find_enclosing_cpu_mapping_offset(
  774. struct kbase_context *kctx, u64 gpu_addr,
  775. unsigned long uaddr, size_t size, u64 *offset)
  776. {
  777. struct kbase_cpu_mapping *map = NULL;
  778. const struct kbase_va_region *reg;
  779. int err = -EINVAL;
  780. KBASE_DEBUG_ASSERT(kctx != NULL);
  781. kbase_gpu_vm_lock(kctx);
  782. reg = kbase_region_tracker_find_region_enclosing_address(kctx, gpu_addr);
  783. if (reg && !(reg->flags & KBASE_REG_FREE)) {
  784. map = kbasep_find_enclosing_cpu_mapping_of_region(reg, uaddr,
  785. size);
  786. if (map) {
  787. *offset = (uaddr - PTR_TO_U64(map->vm_start)) +
  788. (map->page_off << PAGE_SHIFT);
  789. err = 0;
  790. }
  791. }
  792. kbase_gpu_vm_unlock(kctx);
  793. return err;
  794. }
  795. void kbase_sync_single(struct kbase_context *kctx,
  796. phys_addr_t cpu_pa, phys_addr_t gpu_pa,
  797. off_t offset, size_t size, enum kbase_sync_type sync_fn)
  798. {
  799. struct page *cpu_page;
  800. cpu_page = pfn_to_page(PFN_DOWN(cpu_pa));
  801. if (likely(cpu_pa == gpu_pa)) {
  802. dma_addr_t dma_addr;
  803. BUG_ON(!cpu_page);
  804. BUG_ON(offset + size > PAGE_SIZE);
  805. dma_addr = kbase_dma_addr(cpu_page) + offset;
  806. if (sync_fn == KBASE_SYNC_TO_CPU)
  807. dma_sync_single_for_cpu(kctx->kbdev->dev, dma_addr,
  808. size, DMA_BIDIRECTIONAL);
  809. else if (sync_fn == KBASE_SYNC_TO_DEVICE)
  810. dma_sync_single_for_device(kctx->kbdev->dev, dma_addr,
  811. size, DMA_BIDIRECTIONAL);
  812. } else {
  813. void *src = NULL;
  814. void *dst = NULL;
  815. struct page *gpu_page;
  816. if (WARN(!gpu_pa, "No GPU PA found for infinite cache op"))
  817. return;
  818. gpu_page = pfn_to_page(PFN_DOWN(gpu_pa));
  819. if (sync_fn == KBASE_SYNC_TO_DEVICE) {
  820. src = ((unsigned char *)kmap(cpu_page)) + offset;
  821. dst = ((unsigned char *)kmap(gpu_page)) + offset;
  822. } else if (sync_fn == KBASE_SYNC_TO_CPU) {
  823. dma_sync_single_for_cpu(kctx->kbdev->dev,
  824. kbase_dma_addr(gpu_page) + offset,
  825. size, DMA_BIDIRECTIONAL);
  826. src = ((unsigned char *)kmap(gpu_page)) + offset;
  827. dst = ((unsigned char *)kmap(cpu_page)) + offset;
  828. }
  829. memcpy(dst, src, size);
  830. kunmap(gpu_page);
  831. kunmap(cpu_page);
  832. if (sync_fn == KBASE_SYNC_TO_DEVICE)
  833. dma_sync_single_for_device(kctx->kbdev->dev,
  834. kbase_dma_addr(gpu_page) + offset,
  835. size, DMA_BIDIRECTIONAL);
  836. }
  837. }
  838. static int kbase_do_syncset(struct kbase_context *kctx,
  839. struct base_syncset *set, enum kbase_sync_type sync_fn)
  840. {
  841. int err = 0;
  842. struct basep_syncset *sset = &set->basep_sset;
  843. struct kbase_va_region *reg;
  844. struct kbase_cpu_mapping *map;
  845. unsigned long start;
  846. size_t size;
  847. phys_addr_t *cpu_pa;
  848. phys_addr_t *gpu_pa;
  849. u64 page_off, page_count;
  850. u64 i;
  851. off_t offset;
  852. kbase_os_mem_map_lock(kctx);
  853. kbase_gpu_vm_lock(kctx);
  854. /* find the region where the virtual address is contained */
  855. reg = kbase_region_tracker_find_region_enclosing_address(kctx,
  856. sset->mem_handle.basep.handle);
  857. if (!reg) {
  858. dev_warn(kctx->kbdev->dev, "Can't find region at VA 0x%016llX",
  859. sset->mem_handle.basep.handle);
  860. err = -EINVAL;
  861. goto out_unlock;
  862. }
  863. if (!(reg->flags & KBASE_REG_CPU_CACHED))
  864. goto out_unlock;
  865. start = (uintptr_t)sset->user_addr;
  866. size = (size_t)sset->size;
  867. map = kbasep_find_enclosing_cpu_mapping_of_region(reg, start, size);
  868. if (!map) {
  869. dev_warn(kctx->kbdev->dev, "Can't find CPU mapping 0x%016lX for VA 0x%016llX",
  870. start, sset->mem_handle.basep.handle);
  871. err = -EINVAL;
  872. goto out_unlock;
  873. }
  874. offset = start & (PAGE_SIZE - 1);
  875. page_off = map->page_off + ((start - map->vm_start) >> PAGE_SHIFT);
  876. page_count = (size + offset + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  877. cpu_pa = kbase_get_cpu_phy_pages(reg);
  878. gpu_pa = kbase_get_gpu_phy_pages(reg);
  879. /* Sync first page */
  880. if (cpu_pa[page_off]) {
  881. size_t sz = MIN(((size_t) PAGE_SIZE - offset), size);
  882. kbase_sync_single(kctx, cpu_pa[page_off], gpu_pa[page_off],
  883. offset, sz, sync_fn);
  884. }
  885. /* Sync middle pages (if any) */
  886. for (i = 1; page_count > 2 && i < page_count - 1; i++) {
  887. /* we grow upwards, so bail on first non-present page */
  888. if (!cpu_pa[page_off + i])
  889. break;
  890. kbase_sync_single(kctx, cpu_pa[page_off + i],
  891. gpu_pa[page_off + i], 0, PAGE_SIZE, sync_fn);
  892. }
  893. /* Sync last page (if any) */
  894. if (page_count > 1 && cpu_pa[page_off + page_count - 1]) {
  895. size_t sz = ((start + size - 1) & ~PAGE_MASK) + 1;
  896. kbase_sync_single(kctx, cpu_pa[page_off + page_count - 1],
  897. gpu_pa[page_off + page_count - 1], 0, sz,
  898. sync_fn);
  899. }
  900. out_unlock:
  901. kbase_gpu_vm_unlock(kctx);
  902. kbase_os_mem_map_unlock(kctx);
  903. return err;
  904. }
  905. int kbase_sync_now(struct kbase_context *kctx, struct base_syncset *syncset)
  906. {
  907. int err = -EINVAL;
  908. struct basep_syncset *sset;
  909. KBASE_DEBUG_ASSERT(kctx != NULL);
  910. KBASE_DEBUG_ASSERT(syncset != NULL);
  911. sset = &syncset->basep_sset;
  912. switch (sset->type) {
  913. case BASE_SYNCSET_OP_MSYNC:
  914. err = kbase_do_syncset(kctx, syncset, KBASE_SYNC_TO_DEVICE);
  915. break;
  916. case BASE_SYNCSET_OP_CSYNC:
  917. err = kbase_do_syncset(kctx, syncset, KBASE_SYNC_TO_CPU);
  918. break;
  919. default:
  920. dev_warn(kctx->kbdev->dev, "Unknown msync op %d\n", sset->type);
  921. break;
  922. }
  923. return err;
  924. }
  925. /* vm lock must be held */
  926. int kbase_mem_free_region(struct kbase_context *kctx, struct kbase_va_region *reg)
  927. {
  928. int err;
  929. KBASE_DEBUG_ASSERT(kctx != NULL);
  930. KBASE_DEBUG_ASSERT(reg != NULL);
  931. lockdep_assert_held(&kctx->reg_lock);
  932. /*
  933. * Unlink the physical allocation before unmaking it evictable so
  934. * that the allocation isn't grown back to its last backed size
  935. * as we're going to unmap it anyway.
  936. */
  937. reg->cpu_alloc->reg = NULL;
  938. if (reg->cpu_alloc != reg->gpu_alloc)
  939. reg->gpu_alloc->reg = NULL;
  940. /*
  941. * If a region has been made evictable then we must unmake it
  942. * before trying to free it.
  943. * If the memory hasn't been reclaimed it will be unmapped and freed
  944. * below, if it has been reclaimed then the operations below are no-ops.
  945. */
  946. if (reg->flags & KBASE_REG_DONT_NEED) {
  947. KBASE_DEBUG_ASSERT(reg->cpu_alloc->type ==
  948. KBASE_MEM_TYPE_NATIVE);
  949. kbase_mem_evictable_unmake(reg->gpu_alloc);
  950. }
  951. err = kbase_gpu_munmap(kctx, reg);
  952. if (err) {
  953. dev_warn(reg->kctx->kbdev->dev, "Could not unmap from the GPU...\n");
  954. goto out;
  955. }
  956. if (kbase_hw_has_issue(kctx->kbdev, BASE_HW_ISSUE_6367)) {
  957. /* Wait for GPU to flush write buffer before freeing physical pages */
  958. kbase_wait_write_flush(kctx);
  959. }
  960. /* This will also free the physical pages */
  961. kbase_free_alloced_region(reg);
  962. out:
  963. return err;
  964. }
  965. /**
  966. * @brief Free the region from the GPU and unregister it.
  967. *
  968. * This function implements the free operation on a memory segment.
  969. * It will loudly fail if called with outstanding mappings.
  970. */
  971. int kbase_mem_free(struct kbase_context *kctx, u64 gpu_addr)
  972. {
  973. int err = 0;
  974. struct kbase_va_region *reg;
  975. KBASE_DEBUG_ASSERT(kctx != NULL);
  976. if (gpu_addr == 0) {
  977. dev_warn(kctx->kbdev->dev, "gpu_addr 0 is reserved for the ringbuffer and it's an error to try to free it using kbase_mem_free\n");
  978. return -EINVAL;
  979. }
  980. kbase_gpu_vm_lock(kctx);
  981. if (gpu_addr >= BASE_MEM_COOKIE_BASE &&
  982. gpu_addr < BASE_MEM_FIRST_FREE_ADDRESS) {
  983. int cookie = PFN_DOWN(gpu_addr - BASE_MEM_COOKIE_BASE);
  984. reg = kctx->pending_regions[cookie];
  985. if (!reg) {
  986. err = -EINVAL;
  987. goto out_unlock;
  988. }
  989. /* ask to unlink the cookie as we'll free it */
  990. kctx->pending_regions[cookie] = NULL;
  991. kctx->cookies |= (1UL << cookie);
  992. kbase_free_alloced_region(reg);
  993. } else {
  994. /* A real GPU va */
  995. /* Validate the region */
  996. reg = kbase_region_tracker_find_region_base_address(kctx, gpu_addr);
  997. if (!reg || (reg->flags & KBASE_REG_FREE)) {
  998. dev_warn(kctx->kbdev->dev, "kbase_mem_free called with nonexistent gpu_addr 0x%llX",
  999. gpu_addr);
  1000. err = -EINVAL;
  1001. goto out_unlock;
  1002. }
  1003. if ((reg->flags & KBASE_REG_ZONE_MASK) == KBASE_REG_ZONE_SAME_VA) {
  1004. /* SAME_VA must be freed through munmap */
  1005. dev_warn(kctx->kbdev->dev, "%s called on SAME_VA memory 0x%llX", __func__,
  1006. gpu_addr);
  1007. err = -EINVAL;
  1008. goto out_unlock;
  1009. }
  1010. err = kbase_mem_free_region(kctx, reg);
  1011. }
  1012. out_unlock:
  1013. kbase_gpu_vm_unlock(kctx);
  1014. return err;
  1015. }
  1016. void kbase_update_region_flags(struct kbase_context *kctx,
  1017. struct kbase_va_region *reg, unsigned long flags)
  1018. {
  1019. KBASE_DEBUG_ASSERT(reg != NULL);
  1020. KBASE_DEBUG_ASSERT((flags & ~((1ul << BASE_MEM_FLAGS_NR_BITS) - 1)) == 0);
  1021. reg->flags |= kbase_cache_enabled(flags, reg->nr_pages);
  1022. /* all memory is now growable */
  1023. reg->flags |= KBASE_REG_GROWABLE;
  1024. if (flags & BASE_MEM_GROW_ON_GPF)
  1025. reg->flags |= KBASE_REG_PF_GROW;
  1026. if (flags & BASE_MEM_PROT_CPU_WR)
  1027. reg->flags |= KBASE_REG_CPU_WR;
  1028. if (flags & BASE_MEM_PROT_CPU_RD)
  1029. reg->flags |= KBASE_REG_CPU_RD;
  1030. if (flags & BASE_MEM_PROT_GPU_WR)
  1031. reg->flags |= KBASE_REG_GPU_WR;
  1032. if (flags & BASE_MEM_PROT_GPU_RD)
  1033. reg->flags |= KBASE_REG_GPU_RD;
  1034. if (0 == (flags & BASE_MEM_PROT_GPU_EX))
  1035. reg->flags |= KBASE_REG_GPU_NX;
  1036. if (flags & BASE_MEM_COHERENT_SYSTEM ||
  1037. flags & BASE_MEM_COHERENT_SYSTEM_REQUIRED)
  1038. reg->flags |= KBASE_REG_SHARE_BOTH;
  1039. else if (flags & BASE_MEM_COHERENT_LOCAL)
  1040. reg->flags |= KBASE_REG_SHARE_IN;
  1041. /* Set up default MEMATTR usage */
  1042. if (kctx->kbdev->system_coherency == COHERENCY_ACE &&
  1043. (reg->flags & KBASE_REG_SHARE_BOTH)) {
  1044. reg->flags |=
  1045. KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_DEFAULT_ACE);
  1046. } else {
  1047. reg->flags |=
  1048. KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_DEFAULT);
  1049. }
  1050. }
  1051. int kbase_alloc_phy_pages_helper(
  1052. struct kbase_mem_phy_alloc *alloc,
  1053. size_t nr_pages_requested)
  1054. {
  1055. int new_page_count __maybe_unused;
  1056. size_t old_page_count = alloc->nents;
  1057. KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_NATIVE);
  1058. KBASE_DEBUG_ASSERT(alloc->imported.kctx);
  1059. if (nr_pages_requested == 0)
  1060. goto done; /*nothing to do*/
  1061. new_page_count = kbase_atomic_add_pages(
  1062. nr_pages_requested, &alloc->imported.kctx->used_pages);
  1063. kbase_atomic_add_pages(nr_pages_requested, &alloc->imported.kctx->kbdev->memdev.used_pages);
  1064. /* Increase mm counters before we allocate pages so that this
  1065. * allocation is visible to the OOM killer */
  1066. kbase_process_page_usage_inc(alloc->imported.kctx, nr_pages_requested);
  1067. if (kbase_mem_pool_alloc_pages(&alloc->imported.kctx->mem_pool,
  1068. nr_pages_requested, alloc->pages + old_page_count) != 0)
  1069. goto no_alloc;
  1070. /*
  1071. * Request a zone cache update, this scans only the new pages an
  1072. * appends their information to the zone cache. if the update
  1073. * fails then clear the cache so we fall-back to doing things
  1074. * page by page.
  1075. */
  1076. if (kbase_zone_cache_update(alloc, old_page_count) != 0)
  1077. kbase_zone_cache_clear(alloc);
  1078. kbase_tlstream_aux_pagesalloc(
  1079. (u32)alloc->imported.kctx->id,
  1080. (u64)new_page_count);
  1081. alloc->nents += nr_pages_requested;
  1082. done:
  1083. return 0;
  1084. no_alloc:
  1085. kbase_process_page_usage_dec(alloc->imported.kctx, nr_pages_requested);
  1086. kbase_atomic_sub_pages(nr_pages_requested, &alloc->imported.kctx->used_pages);
  1087. kbase_atomic_sub_pages(nr_pages_requested, &alloc->imported.kctx->kbdev->memdev.used_pages);
  1088. return -ENOMEM;
  1089. }
  1090. int kbase_free_phy_pages_helper(
  1091. struct kbase_mem_phy_alloc *alloc,
  1092. size_t nr_pages_to_free)
  1093. {
  1094. struct kbase_context *kctx = alloc->imported.kctx;
  1095. bool syncback;
  1096. bool reclaimed = (alloc->evicted != 0);
  1097. phys_addr_t *start_free;
  1098. int new_page_count __maybe_unused;
  1099. KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_NATIVE);
  1100. KBASE_DEBUG_ASSERT(alloc->imported.kctx);
  1101. KBASE_DEBUG_ASSERT(alloc->nents >= nr_pages_to_free);
  1102. /* early out if nothing to do */
  1103. if (nr_pages_to_free == 0)
  1104. return 0;
  1105. start_free = alloc->pages + alloc->nents - nr_pages_to_free;
  1106. syncback = alloc->properties & KBASE_MEM_PHY_ALLOC_ACCESSED_CACHED;
  1107. /*
  1108. * Clear the zone cache, we don't expect JIT allocations to be
  1109. * shrunk in parts so there is no point trying to optimize for that
  1110. * by scanning for the changes caused by freeing this memory and
  1111. * updating the existing cache entries.
  1112. */
  1113. kbase_zone_cache_clear(alloc);
  1114. kbase_mem_pool_free_pages(&kctx->mem_pool,
  1115. nr_pages_to_free,
  1116. start_free,
  1117. syncback,
  1118. reclaimed);
  1119. alloc->nents -= nr_pages_to_free;
  1120. /*
  1121. * If the allocation was not evicted (i.e. evicted == 0) then
  1122. * the page accounting needs to be done.
  1123. */
  1124. if (!reclaimed) {
  1125. kbase_process_page_usage_dec(kctx, nr_pages_to_free);
  1126. new_page_count = kbase_atomic_sub_pages(nr_pages_to_free,
  1127. &kctx->used_pages);
  1128. kbase_atomic_sub_pages(nr_pages_to_free,
  1129. &kctx->kbdev->memdev.used_pages);
  1130. kbase_tlstream_aux_pagesalloc(
  1131. (u32)kctx->id,
  1132. (u64)new_page_count);
  1133. }
  1134. return 0;
  1135. }
  1136. void kbase_mem_kref_free(struct kref *kref)
  1137. {
  1138. struct kbase_mem_phy_alloc *alloc;
  1139. alloc = container_of(kref, struct kbase_mem_phy_alloc, kref);
  1140. switch (alloc->type) {
  1141. case KBASE_MEM_TYPE_NATIVE: {
  1142. WARN_ON(!alloc->imported.kctx);
  1143. /*
  1144. * The physical allocation must have been removed from the
  1145. * eviction list before trying to free it.
  1146. */
  1147. WARN_ON(!list_empty(&alloc->evict_node));
  1148. kbase_free_phy_pages_helper(alloc, alloc->nents);
  1149. break;
  1150. }
  1151. case KBASE_MEM_TYPE_ALIAS: {
  1152. /* just call put on the underlying phy allocs */
  1153. size_t i;
  1154. struct kbase_aliased *aliased;
  1155. aliased = alloc->imported.alias.aliased;
  1156. if (aliased) {
  1157. for (i = 0; i < alloc->imported.alias.nents; i++)
  1158. if (aliased[i].alloc)
  1159. kbase_mem_phy_alloc_put(aliased[i].alloc);
  1160. vfree(aliased);
  1161. }
  1162. break;
  1163. }
  1164. case KBASE_MEM_TYPE_RAW:
  1165. /* raw pages, external cleanup */
  1166. break;
  1167. #ifdef CONFIG_UMP
  1168. case KBASE_MEM_TYPE_IMPORTED_UMP:
  1169. ump_dd_release(alloc->imported.ump_handle);
  1170. break;
  1171. #endif
  1172. #ifdef CONFIG_DMA_SHARED_BUFFER
  1173. case KBASE_MEM_TYPE_IMPORTED_UMM:
  1174. dma_buf_detach(alloc->imported.umm.dma_buf,
  1175. alloc->imported.umm.dma_attachment);
  1176. dma_buf_put(alloc->imported.umm.dma_buf);
  1177. break;
  1178. #endif
  1179. case KBASE_MEM_TYPE_IMPORTED_USER_BUF:
  1180. if (alloc->imported.user_buf.mm)
  1181. mmdrop(alloc->imported.user_buf.mm);
  1182. kfree(alloc->imported.user_buf.pages);
  1183. break;
  1184. case KBASE_MEM_TYPE_TB:{
  1185. void *tb;
  1186. tb = alloc->imported.kctx->jctx.tb;
  1187. kbase_device_trace_buffer_uninstall(alloc->imported.kctx);
  1188. vfree(tb);
  1189. break;
  1190. }
  1191. default:
  1192. WARN(1, "Unexecpted free of type %d\n", alloc->type);
  1193. break;
  1194. }
  1195. /* Free based on allocation type */
  1196. if (alloc->properties & KBASE_MEM_PHY_ALLOC_LARGE)
  1197. vfree(alloc);
  1198. else
  1199. kfree(alloc);
  1200. }
  1201. int kbase_alloc_phy_pages(struct kbase_va_region *reg, size_t vsize, size_t size)
  1202. {
  1203. KBASE_DEBUG_ASSERT(reg != NULL);
  1204. KBASE_DEBUG_ASSERT(vsize > 0);
  1205. /* validate user provided arguments */
  1206. if (size > vsize || vsize > reg->nr_pages)
  1207. goto out_term;
  1208. /* Prevent vsize*sizeof from wrapping around.
  1209. * For instance, if vsize is 2**29+1, we'll allocate 1 byte and the alloc won't fail.
  1210. */
  1211. if ((size_t) vsize > ((size_t) -1 / sizeof(*reg->cpu_alloc->pages)))
  1212. goto out_term;
  1213. KBASE_DEBUG_ASSERT(vsize != 0);
  1214. if (kbase_alloc_phy_pages_helper(reg->cpu_alloc, size) != 0)
  1215. goto out_term;
  1216. reg->cpu_alloc->reg = reg;
  1217. if (reg->cpu_alloc != reg->gpu_alloc) {
  1218. if (kbase_alloc_phy_pages_helper(reg->gpu_alloc, size) != 0)
  1219. goto out_rollback;
  1220. reg->gpu_alloc->reg = reg;
  1221. }
  1222. return 0;
  1223. out_rollback:
  1224. kbase_free_phy_pages_helper(reg->cpu_alloc, size);
  1225. out_term:
  1226. return -1;
  1227. }
  1228. bool kbase_check_alloc_flags(unsigned long flags)
  1229. {
  1230. /* Only known input flags should be set. */
  1231. if (flags & ~BASE_MEM_FLAGS_INPUT_MASK)
  1232. return false;
  1233. /* At least one flag should be set */
  1234. if (flags == 0)
  1235. return false;
  1236. /* Either the GPU or CPU must be reading from the allocated memory */
  1237. if ((flags & (BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_GPU_RD)) == 0)
  1238. return false;
  1239. /* Either the GPU or CPU must be writing to the allocated memory */
  1240. if ((flags & (BASE_MEM_PROT_CPU_WR | BASE_MEM_PROT_GPU_WR)) == 0)
  1241. return false;
  1242. /* GPU cannot be writing to GPU executable memory and cannot grow the memory on page fault. */
  1243. if ((flags & BASE_MEM_PROT_GPU_EX) && (flags & (BASE_MEM_PROT_GPU_WR | BASE_MEM_GROW_ON_GPF)))
  1244. return false;
  1245. /* GPU should have at least read or write access otherwise there is no
  1246. reason for allocating. */
  1247. if ((flags & (BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR)) == 0)
  1248. return false;
  1249. /* BASE_MEM_IMPORT_SHARED is only valid for imported memory */
  1250. if ((flags & BASE_MEM_IMPORT_SHARED) == BASE_MEM_IMPORT_SHARED)
  1251. return false;
  1252. return true;
  1253. }
  1254. bool kbase_check_import_flags(unsigned long flags)
  1255. {
  1256. /* Only known input flags should be set. */
  1257. if (flags & ~BASE_MEM_FLAGS_INPUT_MASK)
  1258. return false;
  1259. /* At least one flag should be set */
  1260. if (flags == 0)
  1261. return false;
  1262. /* Imported memory cannot be GPU executable */
  1263. if (flags & BASE_MEM_PROT_GPU_EX)
  1264. return false;
  1265. /* Imported memory cannot grow on page fault */
  1266. if (flags & BASE_MEM_GROW_ON_GPF)
  1267. return false;
  1268. /* GPU should have at least read or write access otherwise there is no
  1269. reason for importing. */
  1270. if ((flags & (BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR)) == 0)
  1271. return false;
  1272. /* Secure memory cannot be read by the CPU */
  1273. if ((flags & BASE_MEM_SECURE) && (flags & BASE_MEM_PROT_CPU_RD))
  1274. return false;
  1275. return true;
  1276. }
  1277. /**
  1278. * @brief Acquire the per-context region list lock
  1279. */
  1280. void kbase_gpu_vm_lock(struct kbase_context *kctx)
  1281. {
  1282. KBASE_DEBUG_ASSERT(kctx != NULL);
  1283. mutex_lock(&kctx->reg_lock);
  1284. }
  1285. /**
  1286. * @brief Release the per-context region list lock
  1287. */
  1288. void kbase_gpu_vm_unlock(struct kbase_context *kctx)
  1289. {
  1290. KBASE_DEBUG_ASSERT(kctx != NULL);
  1291. mutex_unlock(&kctx->reg_lock);
  1292. }
  1293. /**
  1294. * kbase_jit_destroy_worker - Deferred worker which frees JIT allocations
  1295. * @work: Work item
  1296. *
  1297. * This function does the work of freeing JIT allocations whose physical
  1298. * backing has been released.
  1299. */
  1300. static void kbase_jit_destroy_worker(struct work_struct *work)
  1301. {
  1302. struct kbase_context *kctx;
  1303. struct kbase_va_region *reg;
  1304. kctx = container_of(work, struct kbase_context, jit_work);
  1305. do {
  1306. mutex_lock(&kctx->jit_lock);
  1307. if (list_empty(&kctx->jit_destroy_head))
  1308. reg = NULL;
  1309. else
  1310. reg = list_first_entry(&kctx->jit_destroy_head,
  1311. struct kbase_va_region, jit_node);
  1312. if (reg) {
  1313. list_del(&reg->jit_node);
  1314. mutex_unlock(&kctx->jit_lock);
  1315. kbase_gpu_vm_lock(kctx);
  1316. kbase_mem_free_region(kctx, reg);
  1317. kbase_gpu_vm_unlock(kctx);
  1318. } else
  1319. mutex_unlock(&kctx->jit_lock);
  1320. } while (reg);
  1321. }
  1322. int kbase_jit_init(struct kbase_context *kctx)
  1323. {
  1324. INIT_LIST_HEAD(&kctx->jit_active_head);
  1325. INIT_LIST_HEAD(&kctx->jit_pool_head);
  1326. INIT_LIST_HEAD(&kctx->jit_destroy_head);
  1327. mutex_init(&kctx->jit_lock);
  1328. INIT_WORK(&kctx->jit_work, kbase_jit_destroy_worker);
  1329. return 0;
  1330. }
  1331. struct kbase_va_region *kbase_jit_allocate(struct kbase_context *kctx,
  1332. struct base_jit_alloc_info *info)
  1333. {
  1334. struct kbase_va_region *reg = NULL;
  1335. struct kbase_va_region *walker;
  1336. struct kbase_va_region *temp;
  1337. size_t current_diff = SIZE_MAX;
  1338. int ret;
  1339. mutex_lock(&kctx->jit_lock);
  1340. /*
  1341. * Scan the pool for an existing allocation which meets our
  1342. * requirements and remove it.
  1343. */
  1344. list_for_each_entry_safe(walker, temp, &kctx->jit_pool_head, jit_node) {
  1345. if (walker->nr_pages >= info->va_pages) {
  1346. size_t min_size, max_size, diff;
  1347. /*
  1348. * The JIT allocations VA requirements have been
  1349. * meet, it's suitable but other allocations
  1350. * might be a better fit.
  1351. */
  1352. min_size = min_t(size_t, walker->gpu_alloc->nents,
  1353. info->commit_pages);
  1354. max_size = max_t(size_t, walker->gpu_alloc->nents,
  1355. info->commit_pages);
  1356. diff = max_size - min_size;
  1357. if (current_diff > diff) {
  1358. current_diff = diff;
  1359. reg = walker;
  1360. }
  1361. /* The allocation is an exact match, stop looking */
  1362. if (current_diff == 0)
  1363. break;
  1364. }
  1365. }
  1366. if (reg) {
  1367. /*
  1368. * Remove the found region from the pool and add it to the
  1369. * active list.
  1370. */
  1371. list_del_init(&reg->jit_node);
  1372. list_add(&reg->jit_node, &kctx->jit_active_head);
  1373. /* Release the jit lock before modifying the allocation */
  1374. mutex_unlock(&kctx->jit_lock);
  1375. kbase_gpu_vm_lock(kctx);
  1376. /* Make the physical backing no longer reclaimable */
  1377. if (!kbase_mem_evictable_unmake(reg->gpu_alloc))
  1378. goto update_failed;
  1379. /* Grow the backing if required */
  1380. if (reg->gpu_alloc->nents < info->commit_pages) {
  1381. size_t delta;
  1382. size_t old_size = reg->gpu_alloc->nents;
  1383. /* Allocate some more pages */
  1384. delta = info->commit_pages - reg->gpu_alloc->nents;
  1385. if (kbase_alloc_phy_pages_helper(reg->gpu_alloc, delta)
  1386. != 0)
  1387. goto update_failed;
  1388. if (reg->cpu_alloc != reg->gpu_alloc) {
  1389. if (kbase_alloc_phy_pages_helper(
  1390. reg->cpu_alloc, delta) != 0) {
  1391. kbase_free_phy_pages_helper(
  1392. reg->gpu_alloc, delta);
  1393. goto update_failed;
  1394. }
  1395. }
  1396. ret = kbase_mem_grow_gpu_mapping(kctx, reg,
  1397. info->commit_pages, old_size);
  1398. /*
  1399. * The grow failed so put the allocation back in the
  1400. * pool and return failure.
  1401. */
  1402. if (ret)
  1403. goto update_failed;
  1404. }
  1405. kbase_gpu_vm_unlock(kctx);
  1406. } else {
  1407. /* No suitable JIT allocation was found so create a new one */
  1408. u64 flags = BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_GPU_RD |
  1409. BASE_MEM_PROT_GPU_WR | BASE_MEM_GROW_ON_GPF |
  1410. BASE_MEM_COHERENT_LOCAL;
  1411. u64 gpu_addr;
  1412. u16 alignment;
  1413. mutex_unlock(&kctx->jit_lock);
  1414. reg = kbase_mem_alloc(kctx, info->va_pages, info->commit_pages,
  1415. info->extent, &flags, &gpu_addr, &alignment);
  1416. if (!reg)
  1417. goto out_unlocked;
  1418. mutex_lock(&kctx->jit_lock);
  1419. list_add(&reg->jit_node, &kctx->jit_active_head);
  1420. mutex_unlock(&kctx->jit_lock);
  1421. }
  1422. return reg;
  1423. update_failed:
  1424. /*
  1425. * An update to an allocation from the pool failed, chances
  1426. * are slim a new allocation would fair any better so return
  1427. * the allocation to the pool and return the function with failure.
  1428. */
  1429. kbase_gpu_vm_unlock(kctx);
  1430. mutex_lock(&kctx->jit_lock);
  1431. list_del_init(&reg->jit_node);
  1432. list_add(&reg->jit_node, &kctx->jit_pool_head);
  1433. mutex_unlock(&kctx->jit_lock);
  1434. out_unlocked:
  1435. return NULL;
  1436. }
  1437. void kbase_jit_free(struct kbase_context *kctx, struct kbase_va_region *reg)
  1438. {
  1439. /* The physical backing of memory in the pool is always reclaimable */
  1440. down_read(&kctx->process_mm->mmap_sem);
  1441. kbase_gpu_vm_lock(kctx);
  1442. kbase_mem_evictable_make(reg->gpu_alloc);
  1443. kbase_gpu_vm_unlock(kctx);
  1444. up_read(&kctx->process_mm->mmap_sem);
  1445. mutex_lock(&kctx->jit_lock);
  1446. list_del_init(&reg->jit_node);
  1447. list_add(&reg->jit_node, &kctx->jit_pool_head);
  1448. mutex_unlock(&kctx->jit_lock);
  1449. }
  1450. void kbase_jit_backing_lost(struct kbase_va_region *reg)
  1451. {
  1452. struct kbase_context *kctx = reg->kctx;
  1453. /*
  1454. * JIT allocations will always be on a list, if the region
  1455. * is not on a list then it's not a JIT allocation.
  1456. */
  1457. if (list_empty(&reg->jit_node))
  1458. return;
  1459. /*
  1460. * Freeing the allocation requires locks we might not be able
  1461. * to take now, so move the allocation to the free list and kick
  1462. * the worker which will do the freeing.
  1463. */
  1464. mutex_lock(&kctx->jit_lock);
  1465. list_del_init(&reg->jit_node);
  1466. list_add(&reg->jit_node, &kctx->jit_destroy_head);
  1467. mutex_unlock(&kctx->jit_lock);
  1468. schedule_work(&kctx->jit_work);
  1469. }
  1470. bool kbase_jit_evict(struct kbase_context *kctx)
  1471. {
  1472. struct kbase_va_region *reg = NULL;
  1473. lockdep_assert_held(&kctx->reg_lock);
  1474. /* Free the oldest allocation from the pool */
  1475. mutex_lock(&kctx->jit_lock);
  1476. if (!list_empty(&kctx->jit_pool_head)) {
  1477. reg = list_entry(kctx->jit_pool_head.prev,
  1478. struct kbase_va_region, jit_node);
  1479. list_del(&reg->jit_node);
  1480. }
  1481. mutex_unlock(&kctx->jit_lock);
  1482. if (reg)
  1483. kbase_mem_free_region(kctx, reg);
  1484. return (reg != NULL);
  1485. }
  1486. void kbase_jit_term(struct kbase_context *kctx)
  1487. {
  1488. struct kbase_va_region *walker;
  1489. /* Free all allocations for this context */
  1490. /*
  1491. * Flush the freeing of allocations whose backing has been freed
  1492. * (i.e. everything in jit_destroy_head).
  1493. */
  1494. cancel_work_sync(&kctx->jit_work);
  1495. kbase_gpu_vm_lock(kctx);
  1496. /* Free all allocations from the pool */
  1497. while (!list_empty(&kctx->jit_pool_head)) {
  1498. walker = list_first_entry(&kctx->jit_pool_head,
  1499. struct kbase_va_region, jit_node);
  1500. list_del(&walker->jit_node);
  1501. kbase_mem_free_region(kctx, walker);
  1502. }
  1503. /* Free all allocations from active list */
  1504. while (!list_empty(&kctx->jit_active_head)) {
  1505. walker = list_first_entry(&kctx->jit_active_head,
  1506. struct kbase_va_region, jit_node);
  1507. list_del(&walker->jit_node);
  1508. kbase_mem_free_region(kctx, walker);
  1509. }
  1510. kbase_gpu_vm_unlock(kctx);
  1511. }
  1512. static int kbase_jd_user_buf_map(struct kbase_context *kctx,
  1513. struct kbase_va_region *reg)
  1514. {
  1515. long pinned_pages;
  1516. struct kbase_mem_phy_alloc *alloc;
  1517. struct page **pages;
  1518. phys_addr_t *pa;
  1519. long i;
  1520. int err = -ENOMEM;
  1521. unsigned long address;
  1522. struct mm_struct *mm;
  1523. struct device *dev;
  1524. unsigned long offset;
  1525. unsigned long local_size;
  1526. alloc = reg->gpu_alloc;
  1527. pa = kbase_get_gpu_phy_pages(reg);
  1528. address = alloc->imported.user_buf.address;
  1529. mm = alloc->imported.user_buf.mm;
  1530. KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF);
  1531. pages = alloc->imported.user_buf.pages;
  1532. pinned_pages = get_user_pages(
  1533. address,
  1534. alloc->imported.user_buf.nr_pages,
  1535. (reg->flags & KBASE_REG_GPU_WR) ? FOLL_WRITE : 0,
  1536. pages, NULL);
  1537. if (pinned_pages <= 0)
  1538. return pinned_pages;
  1539. if (pinned_pages != alloc->imported.user_buf.nr_pages) {
  1540. for (i = 0; i < pinned_pages; i++)
  1541. put_page(pages[i]);
  1542. return -ENOMEM;
  1543. }
  1544. dev = kctx->kbdev->dev;
  1545. offset = address & ~PAGE_MASK;
  1546. local_size = alloc->imported.user_buf.size;
  1547. for (i = 0; i < pinned_pages; i++) {
  1548. dma_addr_t dma_addr;
  1549. unsigned long min;
  1550. min = MIN(PAGE_SIZE - offset, local_size);
  1551. dma_addr = dma_map_page(dev, pages[i],
  1552. offset, min,
  1553. DMA_BIDIRECTIONAL);
  1554. if (dma_mapping_error(dev, dma_addr))
  1555. goto unwind;
  1556. alloc->imported.user_buf.dma_addrs[i] = dma_addr;
  1557. pa[i] = page_to_phys(pages[i]);
  1558. local_size -= min;
  1559. offset = 0;
  1560. }
  1561. alloc->nents = pinned_pages;
  1562. err = kbase_mmu_insert_pages(kctx, reg->start_pfn, pa,
  1563. kbase_reg_current_backed_size(reg),
  1564. reg->flags);
  1565. if (err == 0)
  1566. return 0;
  1567. alloc->nents = 0;
  1568. /* fall down */
  1569. unwind:
  1570. while (i--) {
  1571. dma_unmap_page(kctx->kbdev->dev,
  1572. alloc->imported.user_buf.dma_addrs[i],
  1573. PAGE_SIZE, DMA_BIDIRECTIONAL);
  1574. put_page(pages[i]);
  1575. pages[i] = NULL;
  1576. }
  1577. return err;
  1578. }
  1579. static void kbase_jd_user_buf_unmap(struct kbase_context *kctx,
  1580. struct kbase_mem_phy_alloc *alloc, bool writeable)
  1581. {
  1582. long i;
  1583. struct page **pages;
  1584. unsigned long size = alloc->imported.user_buf.size;
  1585. KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF);
  1586. pages = alloc->imported.user_buf.pages;
  1587. for (i = 0; i < alloc->imported.user_buf.nr_pages; i++) {
  1588. unsigned long local_size;
  1589. dma_addr_t dma_addr = alloc->imported.user_buf.dma_addrs[i];
  1590. local_size = MIN(size, PAGE_SIZE - (dma_addr & ~PAGE_MASK));
  1591. dma_unmap_page(kctx->kbdev->dev, dma_addr, local_size,
  1592. DMA_BIDIRECTIONAL);
  1593. if (writeable)
  1594. set_page_dirty_lock(pages[i]);
  1595. put_page(pages[i]);
  1596. pages[i] = NULL;
  1597. size -= local_size;
  1598. }
  1599. alloc->nents = 0;
  1600. }
  1601. #ifdef CONFIG_DMA_SHARED_BUFFER
  1602. extern int kbase_jd_umm_map(struct kbase_context *kctx,
  1603. struct kbase_va_region *reg)
  1604. {
  1605. struct sg_table *sgt;
  1606. struct scatterlist *s;
  1607. int i;
  1608. phys_addr_t *pa;
  1609. int err;
  1610. size_t count = 0;
  1611. struct kbase_mem_phy_alloc *alloc;
  1612. alloc = reg->gpu_alloc;
  1613. KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_IMPORTED_UMM);
  1614. KBASE_DEBUG_ASSERT(alloc->imported.umm.sgt == NULL);
  1615. sgt = dma_buf_map_attachment(alloc->imported.umm.dma_attachment,
  1616. DMA_BIDIRECTIONAL);
  1617. if (IS_ERR_OR_NULL(sgt))
  1618. return -EINVAL;
  1619. /* save for later */
  1620. alloc->imported.umm.sgt = sgt;
  1621. pa = kbase_get_gpu_phy_pages(reg);
  1622. KBASE_DEBUG_ASSERT(pa);
  1623. for_each_sg(sgt->sgl, s, sgt->nents, i) {
  1624. int j;
  1625. size_t pages = PFN_UP(sg_dma_len(s));
  1626. WARN_ONCE(sg_dma_len(s) & (PAGE_SIZE-1),
  1627. "sg_dma_len(s)=%u is not a multiple of PAGE_SIZE\n",
  1628. sg_dma_len(s));
  1629. WARN_ONCE(sg_dma_address(s) & (PAGE_SIZE-1),
  1630. "sg_dma_address(s)=%llx is not aligned to PAGE_SIZE\n",
  1631. (unsigned long long) sg_dma_address(s));
  1632. for (j = 0; (j < pages) && (count < reg->nr_pages); j++,
  1633. count++)
  1634. *pa++ = sg_dma_address(s) + (j << PAGE_SHIFT);
  1635. WARN_ONCE(j < pages,
  1636. "sg list from dma_buf_map_attachment > dma_buf->size=%zu\n",
  1637. alloc->imported.umm.dma_buf->size);
  1638. }
  1639. if (WARN_ONCE(count < reg->nr_pages,
  1640. "sg list from dma_buf_map_attachment < dma_buf->size=%zu\n",
  1641. alloc->imported.umm.dma_buf->size)) {
  1642. err = -EINVAL;
  1643. goto out;
  1644. }
  1645. /* Update nents as we now have pages to map */
  1646. alloc->nents = count;
  1647. err = kbase_mmu_insert_pages(kctx, reg->start_pfn,
  1648. kbase_get_gpu_phy_pages(reg),
  1649. kbase_reg_current_backed_size(reg),
  1650. reg->flags | KBASE_REG_GPU_WR | KBASE_REG_GPU_RD);
  1651. out:
  1652. if (err) {
  1653. dma_buf_unmap_attachment(alloc->imported.umm.dma_attachment,
  1654. alloc->imported.umm.sgt, DMA_BIDIRECTIONAL);
  1655. alloc->imported.umm.sgt = NULL;
  1656. }
  1657. return err;
  1658. }
  1659. extern void kbase_jd_umm_unmap(struct kbase_context *kctx,
  1660. struct kbase_mem_phy_alloc *alloc)
  1661. {
  1662. KBASE_DEBUG_ASSERT(kctx);
  1663. KBASE_DEBUG_ASSERT(alloc);
  1664. KBASE_DEBUG_ASSERT(alloc->imported.umm.dma_attachment);
  1665. KBASE_DEBUG_ASSERT(alloc->imported.umm.sgt);
  1666. dma_buf_unmap_attachment(alloc->imported.umm.dma_attachment,
  1667. alloc->imported.umm.sgt, DMA_BIDIRECTIONAL);
  1668. alloc->imported.umm.sgt = NULL;
  1669. alloc->nents = 0;
  1670. }
  1671. #endif /* CONFIG_DMA_SHARED_BUFFER */
  1672. #if (defined(CONFIG_KDS) && defined(CONFIG_UMP)) \
  1673. || defined(CONFIG_DMA_SHARED_BUFFER_USES_KDS)
  1674. static void add_kds_resource(struct kds_resource *kds_res,
  1675. struct kds_resource **kds_resources, u32 *kds_res_count,
  1676. unsigned long *kds_access_bitmap, bool exclusive)
  1677. {
  1678. u32 i;
  1679. for (i = 0; i < *kds_res_count; i++) {
  1680. /* Duplicate resource, ignore */
  1681. if (kds_resources[i] == kds_res)
  1682. return;
  1683. }
  1684. kds_resources[*kds_res_count] = kds_res;
  1685. if (exclusive)
  1686. set_bit(*kds_res_count, kds_access_bitmap);
  1687. (*kds_res_count)++;
  1688. }
  1689. #endif
  1690. struct kbase_mem_phy_alloc *kbase_map_external_resource(
  1691. struct kbase_context *kctx, struct kbase_va_region *reg,
  1692. struct mm_struct *locked_mm
  1693. #ifdef CONFIG_KDS
  1694. , u32 *kds_res_count, struct kds_resource **kds_resources,
  1695. unsigned long *kds_access_bitmap, bool exclusive
  1696. #endif
  1697. )
  1698. {
  1699. int err;
  1700. /* decide what needs to happen for this resource */
  1701. switch (reg->gpu_alloc->type) {
  1702. case BASE_MEM_IMPORT_TYPE_USER_BUFFER: {
  1703. if (reg->gpu_alloc->imported.user_buf.mm != locked_mm)
  1704. goto exit;
  1705. reg->gpu_alloc->imported.user_buf.current_mapping_usage_count++;
  1706. if (reg->gpu_alloc->imported.user_buf.current_mapping_usage_count == 1) {
  1707. err = kbase_jd_user_buf_map(kctx, reg);
  1708. if (err) {
  1709. reg->gpu_alloc->imported.user_buf.current_mapping_usage_count--;
  1710. goto exit;
  1711. }
  1712. }
  1713. }
  1714. break;
  1715. case BASE_MEM_IMPORT_TYPE_UMP: {
  1716. #if defined(CONFIG_KDS) && defined(CONFIG_UMP)
  1717. if (kds_res_count) {
  1718. struct kds_resource *kds_res;
  1719. kds_res = ump_dd_kds_resource_get(
  1720. reg->gpu_alloc->imported.ump_handle);
  1721. if (kds_res)
  1722. add_kds_resource(kds_res, kds_resources,
  1723. kds_res_count,
  1724. kds_access_bitmap, exclusive);
  1725. }
  1726. #endif /*defined(CONFIG_KDS) && defined(CONFIG_UMP) */
  1727. break;
  1728. }
  1729. #ifdef CONFIG_DMA_SHARED_BUFFER
  1730. case BASE_MEM_IMPORT_TYPE_UMM: {
  1731. #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
  1732. if (kds_res_count) {
  1733. struct kds_resource *kds_res;
  1734. kds_res = get_dma_buf_kds_resource(
  1735. reg->gpu_alloc->imported.umm.dma_buf);
  1736. if (kds_res)
  1737. add_kds_resource(kds_res, kds_resources,
  1738. kds_res_count,
  1739. kds_access_bitmap, exclusive);
  1740. }
  1741. #endif
  1742. reg->gpu_alloc->imported.umm.current_mapping_usage_count++;
  1743. if (reg->gpu_alloc->imported.umm.current_mapping_usage_count == 1) {
  1744. err = kbase_jd_umm_map(kctx, reg);
  1745. if (err) {
  1746. reg->gpu_alloc->imported.umm.current_mapping_usage_count--;
  1747. goto exit;
  1748. }
  1749. }
  1750. break;
  1751. }
  1752. #endif
  1753. default:
  1754. goto exit;
  1755. }
  1756. return kbase_mem_phy_alloc_get(reg->gpu_alloc);
  1757. exit:
  1758. return NULL;
  1759. }
  1760. void kbase_unmap_external_resource(struct kbase_context *kctx,
  1761. struct kbase_va_region *reg, struct kbase_mem_phy_alloc *alloc)
  1762. {
  1763. switch (alloc->type) {
  1764. #ifdef CONFIG_DMA_SHARED_BUFFER
  1765. case KBASE_MEM_TYPE_IMPORTED_UMM: {
  1766. alloc->imported.umm.current_mapping_usage_count--;
  1767. if (alloc->imported.umm.current_mapping_usage_count == 0) {
  1768. if (reg && reg->gpu_alloc == alloc)
  1769. kbase_mmu_teardown_pages(
  1770. kctx,
  1771. reg->start_pfn,
  1772. kbase_reg_current_backed_size(reg));
  1773. kbase_jd_umm_unmap(kctx, alloc);
  1774. }
  1775. }
  1776. break;
  1777. #endif /* CONFIG_DMA_SHARED_BUFFER */
  1778. case KBASE_MEM_TYPE_IMPORTED_USER_BUF: {
  1779. alloc->imported.user_buf.current_mapping_usage_count--;
  1780. if (alloc->imported.user_buf.current_mapping_usage_count == 0) {
  1781. bool writeable = true;
  1782. if (reg && reg->gpu_alloc == alloc)
  1783. kbase_mmu_teardown_pages(
  1784. kctx,
  1785. reg->start_pfn,
  1786. kbase_reg_current_backed_size(reg));
  1787. if (reg && ((reg->flags & KBASE_REG_GPU_WR) == 0))
  1788. writeable = false;
  1789. kbase_jd_user_buf_unmap(kctx, alloc, writeable);
  1790. }
  1791. }
  1792. break;
  1793. default:
  1794. break;
  1795. }
  1796. kbase_mem_phy_alloc_put(alloc);
  1797. }
  1798. struct kbase_ctx_ext_res_meta *kbase_sticky_resource_acquire(
  1799. struct kbase_context *kctx, u64 gpu_addr)
  1800. {
  1801. struct kbase_ctx_ext_res_meta *meta = NULL;
  1802. struct kbase_ctx_ext_res_meta *walker;
  1803. lockdep_assert_held(&kctx->reg_lock);
  1804. /*
  1805. * Walk the per context external resource metadata list for the
  1806. * metadata which matches the region which is being acquired.
  1807. */
  1808. list_for_each_entry(walker, &kctx->ext_res_meta_head, ext_res_node) {
  1809. if (walker->gpu_addr == gpu_addr) {
  1810. meta = walker;
  1811. break;
  1812. }
  1813. }
  1814. /* No metadata exists so create one. */
  1815. if (!meta) {
  1816. struct kbase_va_region *reg;
  1817. /* Find the region */
  1818. reg = kbase_region_tracker_find_region_enclosing_address(
  1819. kctx, gpu_addr);
  1820. if (NULL == reg || (reg->flags & KBASE_REG_FREE))
  1821. goto failed;
  1822. /* Allocate the metadata object */
  1823. meta = kzalloc(sizeof(*meta), GFP_KERNEL);
  1824. if (!meta)
  1825. goto failed;
  1826. /*
  1827. * Fill in the metadata object and acquire a reference
  1828. * for the physical resource.
  1829. */
  1830. meta->alloc = kbase_map_external_resource(kctx, reg, NULL
  1831. #ifdef CONFIG_KDS
  1832. , NULL, NULL,
  1833. NULL, false
  1834. #endif
  1835. );
  1836. if (!meta->alloc)
  1837. goto fail_map;
  1838. meta->gpu_addr = reg->start_pfn << PAGE_SHIFT;
  1839. list_add(&meta->ext_res_node, &kctx->ext_res_meta_head);
  1840. }
  1841. return meta;
  1842. fail_map:
  1843. kfree(meta);
  1844. failed:
  1845. return NULL;
  1846. }
  1847. bool kbase_sticky_resource_release(struct kbase_context *kctx,
  1848. struct kbase_ctx_ext_res_meta *meta, u64 gpu_addr)
  1849. {
  1850. struct kbase_ctx_ext_res_meta *walker;
  1851. struct kbase_va_region *reg;
  1852. lockdep_assert_held(&kctx->reg_lock);
  1853. /* Search of the metadata if one isn't provided. */
  1854. if (!meta) {
  1855. /*
  1856. * Walk the per context external resource metadata list for the
  1857. * metadata which matches the region which is being released.
  1858. */
  1859. list_for_each_entry(walker, &kctx->ext_res_meta_head,
  1860. ext_res_node) {
  1861. if (walker->gpu_addr == gpu_addr) {
  1862. meta = walker;
  1863. break;
  1864. }
  1865. }
  1866. }
  1867. /* No metadata so just return. */
  1868. if (!meta)
  1869. return false;
  1870. /* Drop the physical memory reference and free the metadata. */
  1871. reg = kbase_region_tracker_find_region_enclosing_address(
  1872. kctx,
  1873. meta->gpu_addr);
  1874. kbase_unmap_external_resource(kctx, reg, meta->alloc);
  1875. list_del(&meta->ext_res_node);
  1876. kfree(meta);
  1877. return true;
  1878. }
  1879. int kbase_sticky_resource_init(struct kbase_context *kctx)
  1880. {
  1881. INIT_LIST_HEAD(&kctx->ext_res_meta_head);
  1882. return 0;
  1883. }
  1884. void kbase_sticky_resource_term(struct kbase_context *kctx)
  1885. {
  1886. struct kbase_ctx_ext_res_meta *walker;
  1887. lockdep_assert_held(&kctx->reg_lock);
  1888. /*
  1889. * Free any sticky resources which haven't been unmapped.
  1890. *
  1891. * Note:
  1892. * We don't care about refcounts at this point as no future
  1893. * references to the meta data will be made.
  1894. * Region termination would find these if we didn't free them
  1895. * here, but it's more efficient if we do the clean up here.
  1896. */
  1897. while (!list_empty(&kctx->ext_res_meta_head)) {
  1898. walker = list_first_entry(&kctx->ext_res_meta_head,
  1899. struct kbase_ctx_ext_res_meta, ext_res_node);
  1900. kbase_sticky_resource_release(kctx, walker, 0);
  1901. }
  1902. }