kgsl_iommu.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/compat.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/qcom-iommu-util.h>
  13. #include <linux/qcom-io-pgtable.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/delay.h>
  16. #include <linux/qcom_scm.h>
  17. #include <linux/random.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <soc/qcom/secure_buffer.h>
  20. #include "adreno.h"
  21. #include "kgsl_device.h"
  22. #include "kgsl_iommu.h"
  23. #include "kgsl_mmu.h"
  24. #include "kgsl_pwrctrl.h"
  25. #include "kgsl_sharedmem.h"
  26. #include "kgsl_trace.h"
  27. #define KGSL_IOMMU_SPLIT_TABLE_BASE 0x0001ff8000000000ULL
  28. #define KGSL_IOMMU_IDR1_OFFSET 0x24
  29. #define IDR1_NUMPAGENDXB GENMASK(30, 28)
  30. #define IDR1_PAGESIZE BIT(31)
  31. static const struct kgsl_mmu_pt_ops secure_pt_ops;
  32. static const struct kgsl_mmu_pt_ops default_pt_ops;
  33. static const struct kgsl_mmu_pt_ops iopgtbl_pt_ops;
  34. /* Zero page for non-secure VBOs */
  35. static struct page *kgsl_vbo_zero_page;
  36. /*
  37. * struct kgsl_iommu_addr_entry - entry in the kgsl_pagetable rbtree.
  38. * @base: starting virtual address of the entry
  39. * @size: size of the entry
  40. * @node: the rbtree node
  41. */
  42. struct kgsl_iommu_addr_entry {
  43. uint64_t base;
  44. uint64_t size;
  45. struct rb_node node;
  46. };
  47. static struct kmem_cache *addr_entry_cache;
  48. /* These are dummy TLB ops for the io-pgtable instances */
  49. static void _tlb_flush_all(void *cookie)
  50. {
  51. }
  52. static void _tlb_flush_walk(unsigned long iova, size_t size,
  53. size_t granule, void *cookie)
  54. {
  55. }
  56. static void _tlb_add_page(struct iommu_iotlb_gather *gather,
  57. unsigned long iova, size_t granule, void *cookie)
  58. {
  59. }
  60. static const struct iommu_flush_ops kgsl_iopgtbl_tlb_ops = {
  61. .tlb_flush_all = _tlb_flush_all,
  62. .tlb_flush_walk = _tlb_flush_walk,
  63. .tlb_add_page = _tlb_add_page,
  64. };
  65. static struct kgsl_iommu_pt *to_iommu_pt(struct kgsl_pagetable *pagetable)
  66. {
  67. return container_of(pagetable, struct kgsl_iommu_pt, base);
  68. }
  69. static u32 get_llcc_flags(struct kgsl_mmu *mmu)
  70. {
  71. if (!test_bit(KGSL_MMU_LLCC_ENABLE, &mmu->features))
  72. return 0;
  73. if (mmu->subtype == KGSL_IOMMU_SMMU_V500)
  74. return 0;
  75. else
  76. return IOMMU_USE_UPSTREAM_HINT;
  77. }
  78. static int _iommu_get_protection_flags(struct kgsl_mmu *mmu,
  79. struct kgsl_memdesc *memdesc)
  80. {
  81. int flags = IOMMU_READ | IOMMU_WRITE | IOMMU_NOEXEC;
  82. flags |= get_llcc_flags(mmu);
  83. if (memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY)
  84. flags &= ~IOMMU_WRITE;
  85. if (memdesc->priv & KGSL_MEMDESC_PRIVILEGED)
  86. flags |= IOMMU_PRIV;
  87. if (memdesc->flags & KGSL_MEMFLAGS_IOCOHERENT)
  88. flags |= IOMMU_CACHE;
  89. if (memdesc->priv & KGSL_MEMDESC_UCODE)
  90. flags &= ~IOMMU_NOEXEC;
  91. return flags;
  92. }
  93. /* Get a scattterlist for the subrange in the child memdesc */
  94. static int get_sg_from_child(struct sg_table *sgt, struct kgsl_memdesc *child,
  95. u64 offset, u64 length)
  96. {
  97. int npages = (length >> PAGE_SHIFT);
  98. int pgoffset = (offset >> PAGE_SHIFT);
  99. struct scatterlist *target_sg;
  100. struct sg_page_iter iter;
  101. int i = 0, ret;
  102. if (child->pages)
  103. return sg_alloc_table_from_pages(sgt,
  104. child->pages + pgoffset, npages, 0,
  105. length, GFP_KERNEL);
  106. ret = sg_alloc_table(sgt, npages, GFP_KERNEL);
  107. if (ret)
  108. return ret;
  109. target_sg = sgt->sgl;
  110. for_each_sgtable_page(child->sgt, &iter, pgoffset) {
  111. sg_set_page(target_sg, sg_page_iter_page(&iter), PAGE_SIZE, 0);
  112. target_sg = sg_next(target_sg);
  113. if (++i == npages)
  114. break;
  115. }
  116. return 0;
  117. }
  118. static struct iommu_domain *to_iommu_domain(struct kgsl_iommu_context *context)
  119. {
  120. return context->domain;
  121. }
  122. static struct kgsl_iommu *to_kgsl_iommu(struct kgsl_pagetable *pt)
  123. {
  124. return &pt->mmu->iommu;
  125. }
  126. /*
  127. * One page allocation for a guard region to protect against over-zealous
  128. * GPU pre-fetch
  129. */
  130. static struct page *kgsl_guard_page;
  131. static struct page *kgsl_secure_guard_page;
  132. static struct page *iommu_get_guard_page(struct kgsl_memdesc *memdesc)
  133. {
  134. if (kgsl_memdesc_is_secured(memdesc)) {
  135. if (!kgsl_secure_guard_page)
  136. kgsl_secure_guard_page = kgsl_alloc_secure_page();
  137. return kgsl_secure_guard_page;
  138. }
  139. if (!kgsl_guard_page)
  140. kgsl_guard_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
  141. __GFP_NORETRY | __GFP_HIGHMEM);
  142. return kgsl_guard_page;
  143. }
  144. static size_t iommu_pgsize(unsigned long pgsize_bitmap, unsigned long iova,
  145. phys_addr_t paddr, size_t size, size_t *count)
  146. {
  147. unsigned int pgsize_idx, pgsize_idx_next;
  148. unsigned long pgsizes;
  149. size_t offset, pgsize, pgsize_next;
  150. unsigned long addr_merge = paddr | iova;
  151. /* Page sizes supported by the hardware and small enough for @size */
  152. pgsizes = pgsize_bitmap & GENMASK(__fls(size), 0);
  153. /* Constrain the page sizes further based on the maximum alignment */
  154. if (likely(addr_merge))
  155. pgsizes &= GENMASK(__ffs(addr_merge), 0);
  156. /* Make sure we have at least one suitable page size */
  157. if (!pgsizes)
  158. return 0;
  159. /* Pick the biggest page size remaining */
  160. pgsize_idx = __fls(pgsizes);
  161. pgsize = BIT(pgsize_idx);
  162. if (!count)
  163. return pgsize;
  164. /* Find the next biggest support page size, if it exists */
  165. pgsizes = pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
  166. if (!pgsizes)
  167. goto out_set_count;
  168. pgsize_idx_next = __ffs(pgsizes);
  169. pgsize_next = BIT(pgsize_idx_next);
  170. /*
  171. * There's no point trying a bigger page size unless the virtual
  172. * and physical addresses are similarly offset within the larger page.
  173. */
  174. if ((iova ^ paddr) & (pgsize_next - 1))
  175. goto out_set_count;
  176. /* Calculate the offset to the next page size alignment boundary */
  177. offset = pgsize_next - (addr_merge & (pgsize_next - 1));
  178. /*
  179. * If size is big enough to accommodate the larger page, reduce
  180. * the number of smaller pages.
  181. */
  182. if (offset + pgsize_next <= size)
  183. size = offset;
  184. out_set_count:
  185. *count = size >> pgsize_idx;
  186. return pgsize;
  187. }
  188. static int _iopgtbl_unmap_pages(struct kgsl_iommu_pt *pt, u64 gpuaddr,
  189. size_t size)
  190. {
  191. struct io_pgtable_ops *ops = pt->pgtbl_ops;
  192. size_t unmapped = 0;
  193. while (unmapped < size) {
  194. size_t ret, size_to_unmap, remaining, pgcount;
  195. remaining = (size - unmapped);
  196. size_to_unmap = iommu_pgsize(pt->info.cfg.pgsize_bitmap,
  197. gpuaddr, gpuaddr, remaining, &pgcount);
  198. if (size_to_unmap == 0)
  199. break;
  200. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  201. ret = qcom_arm_lpae_unmap_pages(ops, gpuaddr, size_to_unmap,
  202. pgcount, NULL);
  203. #else
  204. ret = ops->unmap_pages(ops, gpuaddr, size_to_unmap,
  205. pgcount, NULL);
  206. #endif
  207. if (ret == 0)
  208. break;
  209. gpuaddr += ret;
  210. unmapped += ret;
  211. }
  212. return (unmapped == size) ? 0 : -EINVAL;
  213. }
  214. static void kgsl_iommu_flush_tlb(struct kgsl_mmu *mmu)
  215. {
  216. struct kgsl_iommu *iommu = &mmu->iommu;
  217. iommu_flush_iotlb_all(to_iommu_domain(&iommu->user_context));
  218. /* As LPAC is optional, check LPAC domain is present before flush */
  219. if (iommu->lpac_context.domain)
  220. iommu_flush_iotlb_all(to_iommu_domain(&iommu->lpac_context));
  221. }
  222. static int _iopgtbl_unmap(struct kgsl_iommu_pt *pt, u64 gpuaddr, size_t size)
  223. {
  224. struct io_pgtable_ops *ops = pt->pgtbl_ops;
  225. int ret = 0;
  226. size_t unmapped;
  227. if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_LPAE)) {
  228. ret = _iopgtbl_unmap_pages(pt, gpuaddr, size);
  229. if (ret)
  230. return ret;
  231. goto flush;
  232. }
  233. unmapped = ops->unmap_pages(ops, gpuaddr, PAGE_SIZE,
  234. size >> PAGE_SHIFT, NULL);
  235. if (unmapped != size)
  236. return -EINVAL;
  237. /*
  238. * Skip below logic for 6.1 kernel version and above as
  239. * qcom_skip_tlb_management() API takes care of avoiding
  240. * TLB operations during slumber.
  241. */
  242. flush:
  243. if (KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE) {
  244. struct kgsl_device *device = KGSL_MMU_DEVICE(pt->base.mmu);
  245. /* Skip TLB Operations if GPU is in slumber */
  246. if (mutex_trylock(&device->mutex)) {
  247. if (device->state == KGSL_STATE_SLUMBER) {
  248. mutex_unlock(&device->mutex);
  249. return 0;
  250. }
  251. mutex_unlock(&device->mutex);
  252. }
  253. }
  254. kgsl_iommu_flush_tlb(pt->base.mmu);
  255. return 0;
  256. }
  257. static size_t _iopgtbl_map_sg(struct kgsl_iommu_pt *pt, u64 gpuaddr,
  258. struct sg_table *sgt, int prot)
  259. {
  260. struct io_pgtable_ops *ops = pt->pgtbl_ops;
  261. struct scatterlist *sg;
  262. size_t mapped = 0;
  263. u64 addr = gpuaddr;
  264. int ret, i;
  265. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  266. if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_LPAE)) {
  267. ret = qcom_arm_lpae_map_sg(ops, addr, sgt->sgl, sgt->nents, prot,
  268. GFP_KERNEL, &mapped);
  269. #else
  270. if (ops->map_sg) {
  271. ret = ops->map_sg(ops, addr, sgt->sgl, sgt->nents, prot,
  272. GFP_KERNEL, &mapped);
  273. #endif
  274. if (ret) {
  275. _iopgtbl_unmap(pt, gpuaddr, mapped);
  276. return 0;
  277. }
  278. return mapped;
  279. }
  280. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  281. size_t size = sg->length, map_size = 0;
  282. phys_addr_t phys = sg_phys(sg);
  283. ret = ops->map_pages(ops, addr, phys, PAGE_SIZE, size >> PAGE_SHIFT,
  284. prot, GFP_KERNEL, &map_size);
  285. if (ret) {
  286. _iopgtbl_unmap(pt, gpuaddr, mapped);
  287. return 0;
  288. }
  289. addr += size;
  290. mapped += map_size;
  291. }
  292. return mapped;
  293. }
  294. static void kgsl_iommu_send_tlb_hint(struct kgsl_mmu *mmu, bool hint)
  295. {
  296. struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
  297. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  298. struct kgsl_iommu *iommu = &mmu->iommu;
  299. /*
  300. * Send hint to SMMU driver for skipping TLB operations during slumber.
  301. * This will help to avoid unnecessary cx gdsc toggling.
  302. */
  303. qcom_skip_tlb_management(&iommu->user_context.pdev->dev, hint);
  304. if (iommu->lpac_context.domain)
  305. qcom_skip_tlb_management(&iommu->lpac_context.pdev->dev, hint);
  306. #endif
  307. /*
  308. * TLB operations are skipped during slumber. Incase CX doesn't
  309. * go down, it can result in incorrect translations due to stale
  310. * TLB entries. Flush TLB before boot up to ensure fresh start.
  311. */
  312. if (!hint)
  313. kgsl_iommu_flush_tlb(mmu);
  314. /* TLB hint for GMU domain */
  315. gmu_core_send_tlb_hint(device, hint);
  316. }
  317. static int
  318. kgsl_iopgtbl_map_child(struct kgsl_pagetable *pt, struct kgsl_memdesc *memdesc,
  319. u64 offset, struct kgsl_memdesc *child, u64 child_offset, u64 length)
  320. {
  321. struct kgsl_iommu_pt *iommu_pt = to_iommu_pt(pt);
  322. struct sg_table sgt;
  323. u32 flags;
  324. size_t mapped;
  325. int ret;
  326. ret = get_sg_from_child(&sgt, child, child_offset, length);
  327. if (ret)
  328. return ret;
  329. /* Inherit the flags from the child for this mapping */
  330. flags = _iommu_get_protection_flags(pt->mmu, child);
  331. mapped = _iopgtbl_map_sg(iommu_pt, memdesc->gpuaddr + offset, &sgt, flags);
  332. sg_free_table(&sgt);
  333. return mapped ? 0 : -ENOMEM;
  334. }
  335. static int
  336. kgsl_iopgtbl_unmap_range(struct kgsl_pagetable *pt, struct kgsl_memdesc *memdesc,
  337. u64 offset, u64 length)
  338. {
  339. if (WARN_ON(offset >= memdesc->size ||
  340. (offset + length) > memdesc->size))
  341. return -ERANGE;
  342. return _iopgtbl_unmap(to_iommu_pt(pt), memdesc->gpuaddr + offset,
  343. length);
  344. }
  345. static size_t _iopgtbl_map_page_to_range(struct kgsl_iommu_pt *pt,
  346. struct page *page, u64 gpuaddr, size_t range, int prot)
  347. {
  348. struct io_pgtable_ops *ops = pt->pgtbl_ops;
  349. size_t mapped = 0, map_size = 0;
  350. u64 addr = gpuaddr;
  351. int ret;
  352. while (range) {
  353. ret = ops->map_pages(ops, addr, page_to_phys(page), PAGE_SIZE,
  354. 1, prot, GFP_KERNEL, &map_size);
  355. if (ret) {
  356. _iopgtbl_unmap(pt, gpuaddr, mapped);
  357. return 0;
  358. }
  359. mapped += map_size;
  360. addr += PAGE_SIZE;
  361. range -= PAGE_SIZE;
  362. }
  363. return mapped;
  364. }
  365. static int kgsl_iopgtbl_map_zero_page_to_range(struct kgsl_pagetable *pt,
  366. struct kgsl_memdesc *memdesc, u64 offset, u64 length)
  367. {
  368. /*
  369. * The SMMU only does the PRT compare at the bottom level of the page table, because
  370. * there is not an easy way for the hardware to perform this check at earlier levels.
  371. * Mark this page writable to avoid page faults while writing to it. Since the address
  372. * of this zero page is programmed in PRR register, MMU will intercept any accesses to
  373. * the page before they go to DDR and will terminate the transaction.
  374. */
  375. u32 flags = IOMMU_READ | IOMMU_WRITE | IOMMU_NOEXEC | get_llcc_flags(pt->mmu);
  376. struct kgsl_iommu_pt *iommu_pt = to_iommu_pt(pt);
  377. struct page *page = kgsl_vbo_zero_page;
  378. if (WARN_ON(!page))
  379. return -ENODEV;
  380. if (WARN_ON((offset >= memdesc->size) ||
  381. (offset + length) > memdesc->size))
  382. return -ERANGE;
  383. if (!_iopgtbl_map_page_to_range(iommu_pt, page, memdesc->gpuaddr + offset,
  384. length, flags))
  385. return -ENOMEM;
  386. return 0;
  387. }
  388. static int kgsl_iopgtbl_map(struct kgsl_pagetable *pagetable,
  389. struct kgsl_memdesc *memdesc)
  390. {
  391. struct kgsl_iommu_pt *pt = to_iommu_pt(pagetable);
  392. size_t mapped, padding;
  393. int prot;
  394. /* Get the protection flags for the user context */
  395. prot = _iommu_get_protection_flags(pagetable->mmu, memdesc);
  396. if (!memdesc->sgt) {
  397. struct sg_table sgt;
  398. int ret;
  399. ret = sg_alloc_table_from_pages(&sgt, memdesc->pages,
  400. memdesc->page_count, 0, memdesc->size, GFP_KERNEL);
  401. if (ret)
  402. return ret;
  403. mapped = _iopgtbl_map_sg(pt, memdesc->gpuaddr, &sgt, prot);
  404. sg_free_table(&sgt);
  405. } else {
  406. mapped = _iopgtbl_map_sg(pt, memdesc->gpuaddr, memdesc->sgt,
  407. prot);
  408. }
  409. if (mapped == 0)
  410. return -ENOMEM;
  411. padding = kgsl_memdesc_footprint(memdesc) - mapped;
  412. if (padding) {
  413. struct page *page = iommu_get_guard_page(memdesc);
  414. size_t ret;
  415. if (page)
  416. ret = _iopgtbl_map_page_to_range(pt, page,
  417. memdesc->gpuaddr + mapped, padding,
  418. prot & ~IOMMU_WRITE);
  419. if (!page || !ret) {
  420. _iopgtbl_unmap(pt, memdesc->gpuaddr, mapped);
  421. return -ENOMEM;
  422. }
  423. }
  424. return 0;
  425. }
  426. static int kgsl_iopgtbl_unmap(struct kgsl_pagetable *pagetable,
  427. struct kgsl_memdesc *memdesc)
  428. {
  429. return _iopgtbl_unmap(to_iommu_pt(pagetable), memdesc->gpuaddr,
  430. kgsl_memdesc_footprint(memdesc));
  431. }
  432. static int _iommu_unmap(struct iommu_domain *domain, u64 addr, size_t size)
  433. {
  434. size_t unmapped = 0;
  435. if (!domain)
  436. return 0;
  437. /* Sign extend TTBR1 addresses all the way to avoid warning */
  438. if (addr & (1ULL << 48))
  439. addr |= 0xffff000000000000;
  440. unmapped = iommu_unmap(domain, addr, size);
  441. return (unmapped == size) ? 0 : -ENOMEM;
  442. }
  443. static size_t _iommu_map_page_to_range(struct iommu_domain *domain,
  444. struct page *page, u64 gpuaddr, size_t range, int prot)
  445. {
  446. size_t mapped = 0;
  447. u64 addr = gpuaddr;
  448. if (!page)
  449. return 0;
  450. /* Sign extend TTBR1 addresses all the way to avoid warning */
  451. if (gpuaddr & (1ULL << 48))
  452. gpuaddr |= 0xffff000000000000;
  453. while (range) {
  454. #if (KERNEL_VERSION(6, 2, 0) <= LINUX_VERSION_CODE)
  455. int ret = iommu_map(domain, addr, page_to_phys(page),
  456. PAGE_SIZE, prot, GFP_KERNEL);
  457. #else
  458. int ret = iommu_map(domain, addr, page_to_phys(page),
  459. PAGE_SIZE, prot);
  460. #endif
  461. if (ret) {
  462. iommu_unmap(domain, gpuaddr, mapped);
  463. return 0;
  464. }
  465. addr += PAGE_SIZE;
  466. mapped += PAGE_SIZE;
  467. range -= PAGE_SIZE;
  468. }
  469. return mapped;
  470. }
  471. static size_t _iommu_map_sg(struct iommu_domain *domain, u64 gpuaddr,
  472. struct sg_table *sgt, int prot)
  473. {
  474. /* Sign extend TTBR1 addresses all the way to avoid warning */
  475. if (gpuaddr & (1ULL << 48))
  476. gpuaddr |= 0xffff000000000000;
  477. return kgsl_mmu_map_sg(domain, gpuaddr, sgt->sgl, sgt->orig_nents, prot);
  478. }
  479. static int
  480. _kgsl_iommu_map(struct kgsl_mmu *mmu, struct iommu_domain *domain,
  481. struct kgsl_memdesc *memdesc)
  482. {
  483. int prot = _iommu_get_protection_flags(mmu, memdesc);
  484. size_t mapped, padding;
  485. int ret = 0;
  486. /*
  487. * For paged memory allocated through kgsl, memdesc->pages is not NULL.
  488. * Allocate sgt here just for its map operation. Contiguous memory
  489. * already has its sgt, so no need to allocate it here.
  490. */
  491. if (!memdesc->pages) {
  492. mapped = _iommu_map_sg(domain, memdesc->gpuaddr,
  493. memdesc->sgt, prot);
  494. } else {
  495. struct sg_table sgt;
  496. ret = sg_alloc_table_from_pages(&sgt, memdesc->pages,
  497. memdesc->page_count, 0, memdesc->size, GFP_KERNEL);
  498. if (ret)
  499. return ret;
  500. mapped = _iommu_map_sg(domain, memdesc->gpuaddr, &sgt, prot);
  501. sg_free_table(&sgt);
  502. }
  503. if (!mapped)
  504. return -ENOMEM;
  505. padding = kgsl_memdesc_footprint(memdesc) - mapped;
  506. if (padding) {
  507. struct page *page = iommu_get_guard_page(memdesc);
  508. size_t guard_mapped;
  509. if (page)
  510. guard_mapped = _iommu_map_page_to_range(domain, page,
  511. memdesc->gpuaddr + mapped, padding, prot & ~IOMMU_WRITE);
  512. if (!page || !guard_mapped) {
  513. _iommu_unmap(domain, memdesc->gpuaddr, mapped);
  514. ret = -ENOMEM;
  515. }
  516. }
  517. return ret;
  518. }
  519. static int kgsl_iommu_secure_map(struct kgsl_pagetable *pagetable,
  520. struct kgsl_memdesc *memdesc)
  521. {
  522. struct kgsl_iommu *iommu = &pagetable->mmu->iommu;
  523. struct iommu_domain *domain = to_iommu_domain(&iommu->secure_context);
  524. return _kgsl_iommu_map(pagetable->mmu, domain, memdesc);
  525. }
  526. /*
  527. * Return true if the address is in the TTBR0 region. This is used for cases
  528. * when the "default" pagetable is used for both TTBR0 and TTBR1
  529. */
  530. static bool is_lower_address(struct kgsl_mmu *mmu, u64 addr)
  531. {
  532. return (test_bit(KGSL_MMU_IOPGTABLE, &mmu->features) &&
  533. addr < KGSL_IOMMU_SPLIT_TABLE_BASE);
  534. }
  535. static int _kgsl_iommu_unmap(struct iommu_domain *domain,
  536. struct kgsl_memdesc *memdesc)
  537. {
  538. if (memdesc->size == 0 || memdesc->gpuaddr == 0)
  539. return -EINVAL;
  540. return _iommu_unmap(domain, memdesc->gpuaddr,
  541. kgsl_memdesc_footprint(memdesc));
  542. }
  543. /* Map on the default pagetable and the LPAC pagetable if it exists */
  544. static int kgsl_iommu_default_map(struct kgsl_pagetable *pagetable,
  545. struct kgsl_memdesc *memdesc)
  546. {
  547. struct kgsl_mmu *mmu = pagetable->mmu;
  548. struct kgsl_iommu *iommu = &mmu->iommu;
  549. struct iommu_domain *domain, *lpac;
  550. int ret;
  551. if (is_lower_address(mmu, memdesc->gpuaddr))
  552. return kgsl_iopgtbl_map(pagetable, memdesc);
  553. domain = to_iommu_domain(&iommu->user_context);
  554. /* Map the object to the default GPU domain */
  555. ret = _kgsl_iommu_map(mmu, domain, memdesc);
  556. /* Also map the object to the LPAC domain if it exists */
  557. lpac = to_iommu_domain(&iommu->lpac_context);
  558. if (!ret && lpac) {
  559. ret = _kgsl_iommu_map(mmu, lpac, memdesc);
  560. /* On failure, also unmap from the default domain */
  561. if (ret)
  562. _kgsl_iommu_unmap(domain, memdesc);
  563. }
  564. return ret;
  565. }
  566. static int kgsl_iommu_secure_unmap(struct kgsl_pagetable *pagetable,
  567. struct kgsl_memdesc *memdesc)
  568. {
  569. struct kgsl_iommu *iommu = &pagetable->mmu->iommu;
  570. if (memdesc->size == 0 || memdesc->gpuaddr == 0)
  571. return -EINVAL;
  572. return _kgsl_iommu_unmap(to_iommu_domain(&iommu->secure_context),
  573. memdesc);
  574. }
  575. static int kgsl_iommu_default_unmap(struct kgsl_pagetable *pagetable,
  576. struct kgsl_memdesc *memdesc)
  577. {
  578. struct kgsl_mmu *mmu = pagetable->mmu;
  579. struct kgsl_iommu *iommu = &mmu->iommu;
  580. int ret;
  581. if (memdesc->size == 0 || memdesc->gpuaddr == 0)
  582. return -EINVAL;
  583. if (is_lower_address(mmu, memdesc->gpuaddr))
  584. return kgsl_iopgtbl_unmap(pagetable, memdesc);
  585. /* Unmap from the default domain */
  586. ret = _kgsl_iommu_unmap(to_iommu_domain(&iommu->user_context), memdesc);
  587. /* Unmap from the LPAC domain if it exists */
  588. ret |= _kgsl_iommu_unmap(to_iommu_domain(&iommu->lpac_context), memdesc);
  589. return ret;
  590. }
  591. static bool kgsl_iommu_addr_is_global(struct kgsl_mmu *mmu, u64 addr)
  592. {
  593. if (test_bit(KGSL_MMU_IOPGTABLE, &mmu->features))
  594. return (addr >= KGSL_IOMMU_SPLIT_TABLE_BASE);
  595. return ((addr >= KGSL_IOMMU_GLOBAL_MEM_BASE(mmu)) &&
  596. (addr < KGSL_IOMMU_GLOBAL_MEM_BASE(mmu) +
  597. KGSL_IOMMU_GLOBAL_MEM_SIZE));
  598. }
  599. static void __iomem *kgsl_iommu_reg(struct kgsl_iommu_context *ctx,
  600. u32 offset)
  601. {
  602. struct kgsl_iommu *iommu = KGSL_IOMMU(ctx->kgsldev);
  603. if (!iommu->cb0_offset) {
  604. u32 reg =
  605. readl_relaxed(iommu->regbase + KGSL_IOMMU_IDR1_OFFSET);
  606. iommu->pagesize =
  607. FIELD_GET(IDR1_PAGESIZE, reg) ? SZ_64K : SZ_4K;
  608. /*
  609. * The number of pages in the global address space or
  610. * translation bank address space is 2^(NUMPAGENDXB + 1).
  611. */
  612. iommu->cb0_offset = iommu->pagesize *
  613. (1 << (FIELD_GET(IDR1_NUMPAGENDXB, reg) + 1));
  614. }
  615. return (void __iomem *) (iommu->regbase + iommu->cb0_offset +
  616. (ctx->cb_num * iommu->pagesize) + offset);
  617. }
  618. static u64 KGSL_IOMMU_GET_CTX_REG_Q(struct kgsl_iommu_context *ctx, u32 offset)
  619. {
  620. void __iomem *addr = kgsl_iommu_reg(ctx, offset);
  621. return readq_relaxed(addr);
  622. }
  623. static void KGSL_IOMMU_SET_CTX_REG(struct kgsl_iommu_context *ctx, u32 offset,
  624. u32 val)
  625. {
  626. void __iomem *addr = kgsl_iommu_reg(ctx, offset);
  627. writel_relaxed(val, addr);
  628. }
  629. static u32 KGSL_IOMMU_GET_CTX_REG(struct kgsl_iommu_context *ctx, u32 offset)
  630. {
  631. void __iomem *addr = kgsl_iommu_reg(ctx, offset);
  632. return readl_relaxed(addr);
  633. }
  634. static int kgsl_iommu_get_gpuaddr(struct kgsl_pagetable *pagetable,
  635. struct kgsl_memdesc *memdesc);
  636. static void kgsl_iommu_map_secure_global(struct kgsl_mmu *mmu,
  637. struct kgsl_memdesc *memdesc)
  638. {
  639. if (IS_ERR_OR_NULL(mmu->securepagetable))
  640. return;
  641. if (!memdesc->gpuaddr) {
  642. int ret = kgsl_iommu_get_gpuaddr(mmu->securepagetable,
  643. memdesc);
  644. if (WARN_ON(ret))
  645. return;
  646. }
  647. kgsl_iommu_secure_map(mmu->securepagetable, memdesc);
  648. }
  649. #define KGSL_GLOBAL_MEM_PAGES (KGSL_IOMMU_GLOBAL_MEM_SIZE >> PAGE_SHIFT)
  650. static u64 global_get_offset(struct kgsl_device *device, u64 size,
  651. unsigned long priv)
  652. {
  653. int start = 0, bit;
  654. if (!device->global_map) {
  655. device->global_map =
  656. kcalloc(BITS_TO_LONGS(KGSL_GLOBAL_MEM_PAGES),
  657. sizeof(unsigned long), GFP_KERNEL);
  658. if (!device->global_map)
  659. return (unsigned long) -ENOMEM;
  660. }
  661. if (priv & KGSL_MEMDESC_RANDOM) {
  662. u32 offset = KGSL_GLOBAL_MEM_PAGES - (size >> PAGE_SHIFT);
  663. start = get_random_u32() % offset;
  664. }
  665. while (start >= 0) {
  666. bit = bitmap_find_next_zero_area(device->global_map,
  667. KGSL_GLOBAL_MEM_PAGES, start, size >> PAGE_SHIFT, 0);
  668. if (bit < KGSL_GLOBAL_MEM_PAGES)
  669. break;
  670. /*
  671. * Later implementations might want to randomize this to reduce
  672. * predictability
  673. */
  674. start--;
  675. }
  676. if (WARN_ON(start < 0))
  677. return (unsigned long) -ENOMEM;
  678. bitmap_set(device->global_map, bit, size >> PAGE_SHIFT);
  679. return bit << PAGE_SHIFT;
  680. }
  681. static void kgsl_iommu_map_global(struct kgsl_mmu *mmu,
  682. struct kgsl_memdesc *memdesc, u32 padding)
  683. {
  684. struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
  685. if (memdesc->flags & KGSL_MEMFLAGS_SECURE) {
  686. kgsl_iommu_map_secure_global(mmu, memdesc);
  687. return;
  688. }
  689. if (!memdesc->gpuaddr) {
  690. u64 offset;
  691. offset = global_get_offset(device, memdesc->size + padding,
  692. memdesc->priv);
  693. if (IS_ERR_VALUE(offset))
  694. return;
  695. memdesc->gpuaddr = mmu->defaultpagetable->global_base + offset;
  696. }
  697. kgsl_iommu_default_map(mmu->defaultpagetable, memdesc);
  698. }
  699. /* Print the mem entry for the pagefault debugging */
  700. static void print_entry(struct device *dev, struct kgsl_mem_entry *entry,
  701. pid_t pid)
  702. {
  703. char name[32];
  704. if (!entry) {
  705. dev_crit(dev, "**EMPTY**\n");
  706. return;
  707. }
  708. kgsl_get_memory_usage(name, sizeof(name), entry->memdesc.flags);
  709. dev_err(dev, "[%016llX - %016llX] %s %s (pid = %d) (%s)\n",
  710. entry->memdesc.gpuaddr,
  711. entry->memdesc.gpuaddr + entry->memdesc.size - 1,
  712. entry->memdesc.priv & KGSL_MEMDESC_GUARD_PAGE ? "(+guard)" : "",
  713. entry->pending_free ? "(pending free)" : "",
  714. pid, name);
  715. }
  716. /* Check if the address in the list of recently freed memory */
  717. static void kgsl_iommu_check_if_freed(struct device *dev,
  718. struct kgsl_iommu_context *context, u64 addr, u32 ptname)
  719. {
  720. uint64_t gpuaddr = addr;
  721. uint64_t size = 0;
  722. uint64_t flags = 0;
  723. char name[32];
  724. pid_t pid;
  725. if (!kgsl_memfree_find_entry(ptname, &gpuaddr, &size, &flags, &pid))
  726. return;
  727. kgsl_get_memory_usage(name, sizeof(name), flags);
  728. dev_err(dev, "---- premature free ----\n");
  729. dev_err(dev, "[%8.8llX-%8.8llX] (%s) was already freed by pid %d\n",
  730. gpuaddr, gpuaddr + size, name, pid);
  731. }
  732. static struct kgsl_process_private *kgsl_iommu_get_process(u64 ptbase)
  733. {
  734. struct kgsl_process_private *p;
  735. struct kgsl_iommu_pt *iommu_pt;
  736. read_lock(&kgsl_driver.proclist_lock);
  737. list_for_each_entry(p, &kgsl_driver.process_list, list) {
  738. iommu_pt = to_iommu_pt(p->pagetable);
  739. if (iommu_pt->ttbr0 == MMU_SW_PT_BASE(ptbase)) {
  740. if (!kgsl_process_private_get(p))
  741. p = NULL;
  742. read_unlock(&kgsl_driver.proclist_lock);
  743. return p;
  744. }
  745. }
  746. read_unlock(&kgsl_driver.proclist_lock);
  747. return NULL;
  748. }
  749. static void kgsl_iommu_add_fault_info(struct kgsl_context *context,
  750. unsigned long addr, int flags)
  751. {
  752. struct kgsl_pagefault_report *report;
  753. u32 fault_flag = 0;
  754. if (!context || !(context->flags & KGSL_CONTEXT_FAULT_INFO))
  755. return;
  756. report = kzalloc(sizeof(struct kgsl_pagefault_report), GFP_KERNEL);
  757. if (!report)
  758. return;
  759. if (flags & IOMMU_FAULT_TRANSLATION)
  760. fault_flag = KGSL_PAGEFAULT_TYPE_TRANSLATION;
  761. else if (flags & IOMMU_FAULT_PERMISSION)
  762. fault_flag = KGSL_PAGEFAULT_TYPE_PERMISSION;
  763. else if (flags & IOMMU_FAULT_EXTERNAL)
  764. fault_flag = KGSL_PAGEFAULT_TYPE_EXTERNAL;
  765. else if (flags & IOMMU_FAULT_TRANSACTION_STALLED)
  766. fault_flag = KGSL_PAGEFAULT_TYPE_TRANSACTION_STALLED;
  767. fault_flag |= (flags & IOMMU_FAULT_WRITE) ? KGSL_PAGEFAULT_TYPE_WRITE :
  768. KGSL_PAGEFAULT_TYPE_READ;
  769. report->fault_addr = addr;
  770. report->fault_type = fault_flag;
  771. if (kgsl_add_fault(context, KGSL_FAULT_TYPE_PAGEFAULT, report))
  772. kfree(report);
  773. }
  774. static void kgsl_iommu_print_fault(struct kgsl_mmu *mmu,
  775. struct kgsl_iommu_context *ctxt, unsigned long addr,
  776. u64 ptbase, u32 contextid,
  777. int flags, struct kgsl_process_private *private,
  778. struct kgsl_context *context)
  779. {
  780. struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
  781. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  782. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  783. struct kgsl_mem_entry *prev = NULL, *next = NULL, *entry;
  784. const char *fault_type = NULL;
  785. const char *comm = NULL;
  786. u32 ptname = KGSL_MMU_GLOBAL_PT;
  787. int id;
  788. if (private) {
  789. comm = private->comm;
  790. ptname = pid_nr(private->pid);
  791. }
  792. trace_kgsl_mmu_pagefault(device, addr,
  793. ptname, comm,
  794. (flags & IOMMU_FAULT_WRITE) ? "write" : "read");
  795. if (flags & IOMMU_FAULT_TRANSLATION)
  796. fault_type = "translation";
  797. else if (flags & IOMMU_FAULT_PERMISSION)
  798. fault_type = "permission";
  799. else if (flags & IOMMU_FAULT_EXTERNAL)
  800. fault_type = "external";
  801. else if (flags & IOMMU_FAULT_TRANSACTION_STALLED)
  802. fault_type = "transaction stalled";
  803. else
  804. fault_type = "unknown";
  805. /* FIXME: This seems buggy */
  806. if (test_bit(KGSL_FT_PAGEFAULT_LOG_ONE_PER_PAGE, &mmu->pfpolicy))
  807. if (!kgsl_mmu_log_fault_addr(mmu, ptbase, addr))
  808. return;
  809. if (!__ratelimit(&ctxt->ratelimit))
  810. return;
  811. dev_crit(device->dev,
  812. "GPU PAGE FAULT: addr = %lX pid= %d name=%s drawctxt=%d context pid = %d\n", addr,
  813. ptname, comm, contextid, context ? context->tid : 0);
  814. dev_crit(device->dev,
  815. "context=%s TTBR0=0x%llx (%s %s fault)\n",
  816. ctxt->name, ptbase,
  817. (flags & IOMMU_FAULT_WRITE) ? "write" : "read", fault_type);
  818. if (gpudev->iommu_fault_block) {
  819. u32 fsynr1 = KGSL_IOMMU_GET_CTX_REG(ctxt,
  820. KGSL_IOMMU_CTX_FSYNR1);
  821. dev_crit(device->dev,
  822. "FAULTING BLOCK: %s\n",
  823. gpudev->iommu_fault_block(device, fsynr1));
  824. }
  825. /* Don't print the debug if this is a permissions fault */
  826. if ((flags & IOMMU_FAULT_PERMISSION))
  827. return;
  828. kgsl_iommu_check_if_freed(device->dev, ctxt, addr, ptname);
  829. /*
  830. * Don't print any debug information if the address is
  831. * in the global region. These are rare and nobody needs
  832. * to know the addresses that are in here
  833. */
  834. if (kgsl_iommu_addr_is_global(mmu, addr)) {
  835. dev_crit(device->dev, "Fault in global memory\n");
  836. return;
  837. }
  838. if (!private)
  839. return;
  840. dev_crit(device->dev, "---- nearby memory ----\n");
  841. spin_lock(&private->mem_lock);
  842. idr_for_each_entry(&private->mem_idr, entry, id) {
  843. u64 cur = entry->memdesc.gpuaddr;
  844. if (cur < addr) {
  845. if (!prev || prev->memdesc.gpuaddr < cur)
  846. prev = entry;
  847. }
  848. if (cur > addr) {
  849. if (!next || next->memdesc.gpuaddr > cur)
  850. next = entry;
  851. }
  852. }
  853. print_entry(device->dev, prev, pid_nr(private->pid));
  854. dev_crit(device->dev, "<- fault @ %16.16lx\n", addr);
  855. print_entry(device->dev, next, pid_nr(private->pid));
  856. spin_unlock(&private->mem_lock);
  857. }
  858. /*
  859. * Return true if the IOMMU should stall and trigger a snapshot on a pagefault
  860. */
  861. static bool kgsl_iommu_check_stall_on_fault(struct kgsl_iommu_context *ctx,
  862. struct kgsl_mmu *mmu, int flags)
  863. {
  864. struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
  865. if (!(flags & IOMMU_FAULT_TRANSACTION_STALLED))
  866. return false;
  867. if (!test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &mmu->pfpolicy))
  868. return false;
  869. if (test_bit(KGSL_MMU_PAGEFAULT_TERMINATE, &mmu->features))
  870. return false;
  871. /*
  872. * Sometimes, there can be multiple invocations of the fault handler.
  873. * Make sure we trigger reset/recovery only once.
  874. */
  875. if (ctx->stalled_on_fault)
  876. return false;
  877. if (!mutex_trylock(&device->mutex))
  878. return true;
  879. /*
  880. * Turn off GPU IRQ so we don't get faults from it too.
  881. * The device mutex must be held to change power state
  882. */
  883. if (gmu_core_isenabled(device))
  884. kgsl_pwrctrl_irq(device, false);
  885. else
  886. kgsl_pwrctrl_change_state(device, KGSL_STATE_AWARE);
  887. mutex_unlock(&device->mutex);
  888. return true;
  889. }
  890. static int kgsl_iommu_fault_handler(struct kgsl_mmu *mmu,
  891. struct kgsl_iommu_context *ctx, unsigned long addr, int flags)
  892. {
  893. struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
  894. u64 ptbase;
  895. u32 contextidr;
  896. bool stall;
  897. struct kgsl_process_private *private;
  898. struct kgsl_context *context;
  899. ptbase = KGSL_IOMMU_GET_CTX_REG_Q(ctx, KGSL_IOMMU_CTX_TTBR0);
  900. contextidr = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_CONTEXTIDR);
  901. private = kgsl_iommu_get_process(ptbase);
  902. context = kgsl_context_get(device, contextidr);
  903. stall = kgsl_iommu_check_stall_on_fault(ctx, mmu, flags);
  904. kgsl_iommu_print_fault(mmu, ctx, addr, ptbase, contextidr, flags, private,
  905. context);
  906. kgsl_iommu_add_fault_info(context, addr, flags);
  907. if (stall) {
  908. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  909. u32 sctlr;
  910. /*
  911. * Disable context fault interrupts as we do not clear FSR in
  912. * the ISR. Will be re-enabled after FSR is cleared.
  913. */
  914. sctlr = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR);
  915. sctlr &= ~(0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT);
  916. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR, sctlr);
  917. /* This is used by reset/recovery path */
  918. ctx->stalled_on_fault = true;
  919. /* Go ahead with recovery*/
  920. if (adreno_dev->dispatch_ops && adreno_dev->dispatch_ops->fault)
  921. adreno_dev->dispatch_ops->fault(adreno_dev,
  922. ADRENO_IOMMU_PAGE_FAULT);
  923. }
  924. kgsl_context_put(context);
  925. kgsl_process_private_put(private);
  926. /* Return -EBUSY to keep the IOMMU driver from resuming on a stall */
  927. return stall ? -EBUSY : 0;
  928. }
  929. static int kgsl_iommu_default_fault_handler(struct iommu_domain *domain,
  930. struct device *dev, unsigned long addr, int flags, void *token)
  931. {
  932. struct kgsl_mmu *mmu = token;
  933. struct kgsl_iommu *iommu = &mmu->iommu;
  934. return kgsl_iommu_fault_handler(mmu, &iommu->user_context,
  935. addr, flags);
  936. }
  937. static int kgsl_iommu_lpac_fault_handler(struct iommu_domain *domain,
  938. struct device *dev, unsigned long addr, int flags, void *token)
  939. {
  940. struct kgsl_mmu *mmu = token;
  941. struct kgsl_iommu *iommu = &mmu->iommu;
  942. struct kgsl_iommu_context *ctx = &iommu->lpac_context;
  943. u32 fsynr0, fsynr1;
  944. fsynr0 = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_FSYNR0);
  945. fsynr1 = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_FSYNR1);
  946. dev_crit(KGSL_MMU_DEVICE(mmu)->dev,
  947. "LPAC PAGE FAULT iova=0x%16lx, fsynr0=0x%x, fsynr1=0x%x\n",
  948. addr, fsynr0, fsynr1);
  949. return kgsl_iommu_fault_handler(mmu, &iommu->lpac_context,
  950. addr, flags);
  951. }
  952. static int kgsl_iommu_secure_fault_handler(struct iommu_domain *domain,
  953. struct device *dev, unsigned long addr, int flags, void *token)
  954. {
  955. struct kgsl_mmu *mmu = token;
  956. struct kgsl_iommu *iommu = &mmu->iommu;
  957. return kgsl_iommu_fault_handler(mmu, &iommu->secure_context,
  958. addr, flags);
  959. }
  960. /*
  961. * kgsl_iommu_disable_clk() - Disable iommu clocks
  962. * Disable IOMMU clocks
  963. */
  964. static void kgsl_iommu_disable_clk(struct kgsl_mmu *mmu)
  965. {
  966. struct kgsl_iommu *iommu = &mmu->iommu;
  967. atomic_dec(&iommu->clk_enable_count);
  968. /*
  969. * Make sure the clk refcounts are good. An unbalance may
  970. * cause the clocks to be off when we need them on.
  971. */
  972. WARN_ON(atomic_read(&iommu->clk_enable_count) < 0);
  973. clk_bulk_disable_unprepare(iommu->num_clks, iommu->clks);
  974. if (!IS_ERR_OR_NULL(iommu->cx_gdsc))
  975. regulator_disable(iommu->cx_gdsc);
  976. }
  977. /*
  978. * kgsl_iommu_enable_clk - Enable iommu clocks
  979. * Enable all the IOMMU clocks
  980. */
  981. static void kgsl_iommu_enable_clk(struct kgsl_mmu *mmu)
  982. {
  983. struct kgsl_iommu *iommu = &mmu->iommu;
  984. if (!IS_ERR_OR_NULL(iommu->cx_gdsc))
  985. WARN_ON(regulator_enable(iommu->cx_gdsc));
  986. WARN_ON(clk_bulk_prepare_enable(iommu->num_clks, iommu->clks));
  987. atomic_inc(&iommu->clk_enable_count);
  988. }
  989. /* kgsl_iommu_get_ttbr0 - Get TTBR0 setting for a pagetable */
  990. static u64 kgsl_iommu_get_ttbr0(struct kgsl_pagetable *pagetable)
  991. {
  992. struct kgsl_iommu_pt *pt = to_iommu_pt(pagetable);
  993. /* This will be zero if KGSL_MMU_IOPGTABLE is not enabled */
  994. return pt->ttbr0;
  995. }
  996. /* Set TTBR0 for the given context with the specific configuration */
  997. static void kgsl_iommu_set_ttbr0(struct kgsl_iommu_context *context,
  998. struct kgsl_mmu *mmu, const struct io_pgtable_cfg *pgtbl_cfg)
  999. {
  1000. struct adreno_smmu_priv *adreno_smmu;
  1001. /* Quietly return if the context doesn't have a domain */
  1002. if (!context->domain)
  1003. return;
  1004. adreno_smmu = dev_get_drvdata(&context->pdev->dev);
  1005. /* Enable CX and clocks before we call into SMMU to setup registers */
  1006. kgsl_iommu_enable_clk(mmu);
  1007. adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, pgtbl_cfg);
  1008. kgsl_iommu_disable_clk(mmu);
  1009. }
  1010. static int kgsl_iommu_get_context_bank(struct kgsl_pagetable *pt, struct kgsl_context *context)
  1011. {
  1012. struct kgsl_iommu *iommu = to_kgsl_iommu(pt);
  1013. struct iommu_domain *domain;
  1014. if (kgsl_context_is_lpac(context))
  1015. domain = to_iommu_domain(&iommu->lpac_context);
  1016. else
  1017. domain = to_iommu_domain(&iommu->user_context);
  1018. return qcom_iommu_get_context_bank_nr(domain);
  1019. }
  1020. static int kgsl_iommu_get_asid(struct kgsl_pagetable *pt, struct kgsl_context *context)
  1021. {
  1022. struct kgsl_iommu *iommu = to_kgsl_iommu(pt);
  1023. struct iommu_domain *domain;
  1024. if (kgsl_context_is_lpac(context))
  1025. domain = to_iommu_domain(&iommu->lpac_context);
  1026. else
  1027. domain = to_iommu_domain(&iommu->user_context);
  1028. return qcom_iommu_get_asid_nr(domain);
  1029. }
  1030. static void kgsl_iommu_destroy_default_pagetable(struct kgsl_pagetable *pagetable)
  1031. {
  1032. struct kgsl_device *device = KGSL_MMU_DEVICE(pagetable->mmu);
  1033. struct kgsl_iommu_pt *pt = to_iommu_pt(pagetable);
  1034. struct kgsl_global_memdesc *md;
  1035. list_for_each_entry(md, &device->globals, node) {
  1036. if (md->memdesc.flags & KGSL_MEMFLAGS_SECURE)
  1037. continue;
  1038. kgsl_iommu_default_unmap(pagetable, &md->memdesc);
  1039. }
  1040. kfree(pt);
  1041. }
  1042. static void kgsl_iommu_destroy_pagetable(struct kgsl_pagetable *pagetable)
  1043. {
  1044. struct kgsl_iommu_pt *pt = to_iommu_pt(pagetable);
  1045. qcom_free_io_pgtable_ops(pt->pgtbl_ops);
  1046. kfree(pt);
  1047. }
  1048. static void _enable_gpuhtw_llc(struct kgsl_mmu *mmu, struct iommu_domain *domain)
  1049. {
  1050. if (!test_bit(KGSL_MMU_LLCC_ENABLE, &mmu->features))
  1051. return;
  1052. if (mmu->subtype == KGSL_IOMMU_SMMU_V500) {
  1053. if (!test_bit(KGSL_MMU_IO_COHERENT, &mmu->features))
  1054. iommu_set_pgtable_quirks(domain,
  1055. IO_PGTABLE_QUIRK_QCOM_USE_LLC_NWA);
  1056. } else
  1057. iommu_set_pgtable_quirks(domain, IO_PGTABLE_QUIRK_ARM_OUTER_WBWA);
  1058. }
  1059. int kgsl_set_smmu_aperture(struct kgsl_device *device,
  1060. struct kgsl_iommu_context *context)
  1061. {
  1062. int ret;
  1063. if (!test_bit(KGSL_MMU_SMMU_APERTURE, &device->mmu.features))
  1064. return 0;
  1065. ret = qcom_scm_kgsl_set_smmu_aperture(context->cb_num);
  1066. if (ret == -EBUSY)
  1067. ret = qcom_scm_kgsl_set_smmu_aperture(context->cb_num);
  1068. if (ret)
  1069. dev_err(&device->pdev->dev, "Unable to set the SMMU aperture: %d. The aperture needs to be set to use per-process pagetables\n",
  1070. ret);
  1071. return ret;
  1072. }
  1073. int kgsl_set_smmu_lpac_aperture(struct kgsl_device *device,
  1074. struct kgsl_iommu_context *context)
  1075. {
  1076. int ret;
  1077. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  1078. if (!test_bit(KGSL_MMU_SMMU_APERTURE, &device->mmu.features) ||
  1079. !ADRENO_FEATURE(adreno_dev, ADRENO_LPAC))
  1080. return 0;
  1081. ret = qcom_scm_kgsl_set_smmu_lpac_aperture(context->cb_num);
  1082. if (ret == -EBUSY)
  1083. ret = qcom_scm_kgsl_set_smmu_lpac_aperture(context->cb_num);
  1084. if (ret)
  1085. dev_err(&device->pdev->dev, "Unable to set the LPAC SMMU aperture: %d. The aperture needs to be set to use per-process pagetables\n",
  1086. ret);
  1087. return ret;
  1088. }
  1089. /* FIXME: better name feor this function */
  1090. static int kgsl_iopgtbl_alloc(struct kgsl_iommu_context *ctx, struct kgsl_iommu_pt *pt)
  1091. {
  1092. struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(&ctx->pdev->dev);
  1093. const struct io_pgtable_cfg *cfg = NULL;
  1094. void *domain = (void *)adreno_smmu->cookie;
  1095. if (adreno_smmu->cookie)
  1096. cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
  1097. if (!cfg)
  1098. return -ENODEV;
  1099. pt->info = adreno_smmu->pgtbl_info;
  1100. pt->info.cfg = *cfg;
  1101. pt->info.cfg.quirks &= ~IO_PGTABLE_QUIRK_ARM_TTBR1;
  1102. pt->info.cfg.tlb = &kgsl_iopgtbl_tlb_ops;
  1103. pt->pgtbl_ops = qcom_alloc_io_pgtable_ops(QCOM_ARM_64_LPAE_S1, &pt->info, domain);
  1104. if (!pt->pgtbl_ops)
  1105. return -ENOMEM;
  1106. pt->ttbr0 = pt->info.cfg.arm_lpae_s1_cfg.ttbr;
  1107. return 0;
  1108. }
  1109. static struct kgsl_pagetable *kgsl_iommu_default_pagetable(struct kgsl_mmu *mmu)
  1110. {
  1111. struct kgsl_iommu *iommu = &mmu->iommu;
  1112. struct kgsl_iommu_pt *iommu_pt;
  1113. int ret;
  1114. iommu_pt = kzalloc(sizeof(*iommu_pt), GFP_KERNEL);
  1115. if (!iommu_pt)
  1116. return ERR_PTR(-ENOMEM);
  1117. kgsl_mmu_pagetable_init(mmu, &iommu_pt->base, KGSL_MMU_GLOBAL_PT);
  1118. iommu_pt->base.fault_addr = U64_MAX;
  1119. iommu_pt->base.rbtree = RB_ROOT;
  1120. iommu_pt->base.pt_ops = &default_pt_ops;
  1121. if (test_bit(KGSL_MMU_64BIT, &mmu->features)) {
  1122. iommu_pt->base.compat_va_start = KGSL_IOMMU_SVM_BASE32(mmu);
  1123. iommu_pt->base.compat_va_end = KGSL_IOMMU_SVM_END32(mmu);
  1124. iommu_pt->base.va_start = KGSL_IOMMU_VA_BASE64;
  1125. iommu_pt->base.va_end = KGSL_IOMMU_VA_END64;
  1126. } else {
  1127. iommu_pt->base.va_start = KGSL_IOMMU_SVM_BASE32(mmu);
  1128. iommu_pt->base.va_end = KGSL_IOMMU_GLOBAL_MEM_BASE(mmu);
  1129. iommu_pt->base.compat_va_start = iommu_pt->base.va_start;
  1130. iommu_pt->base.compat_va_end = iommu_pt->base.va_end;
  1131. }
  1132. if (!test_bit(KGSL_MMU_IOPGTABLE, &mmu->features)) {
  1133. iommu_pt->base.global_base = KGSL_IOMMU_GLOBAL_MEM_BASE(mmu);
  1134. kgsl_mmu_pagetable_add(mmu, &iommu_pt->base);
  1135. return &iommu_pt->base;
  1136. }
  1137. iommu_pt->base.global_base = KGSL_IOMMU_SPLIT_TABLE_BASE;
  1138. /*
  1139. * Set up a "default' TTBR0 for the pagetable - this would only be used
  1140. * in cases when the per-process pagetable allocation failed for some
  1141. * reason
  1142. */
  1143. ret = kgsl_iopgtbl_alloc(&iommu->user_context, iommu_pt);
  1144. if (ret) {
  1145. kfree(iommu_pt);
  1146. return ERR_PTR(ret);
  1147. }
  1148. kgsl_mmu_pagetable_add(mmu, &iommu_pt->base);
  1149. return &iommu_pt->base;
  1150. }
  1151. #if IS_ENABLED(CONFIG_QCOM_SECURE_BUFFER)
  1152. static struct kgsl_pagetable *kgsl_iommu_secure_pagetable(struct kgsl_mmu *mmu)
  1153. {
  1154. struct kgsl_iommu_pt *iommu_pt;
  1155. if (!mmu->secured)
  1156. return ERR_PTR(-EPERM);
  1157. iommu_pt = kzalloc(sizeof(*iommu_pt), GFP_KERNEL);
  1158. if (!iommu_pt)
  1159. return ERR_PTR(-ENOMEM);
  1160. kgsl_mmu_pagetable_init(mmu, &iommu_pt->base, KGSL_MMU_SECURE_PT);
  1161. iommu_pt->base.fault_addr = U64_MAX;
  1162. iommu_pt->base.rbtree = RB_ROOT;
  1163. iommu_pt->base.pt_ops = &secure_pt_ops;
  1164. iommu_pt->base.compat_va_start = KGSL_IOMMU_SECURE_BASE32;
  1165. iommu_pt->base.compat_va_end = KGSL_IOMMU_SECURE_END32;
  1166. iommu_pt->base.va_start = KGSL_IOMMU_SECURE_BASE(mmu);
  1167. iommu_pt->base.va_end = KGSL_IOMMU_SECURE_END(mmu);
  1168. kgsl_mmu_pagetable_add(mmu, &iommu_pt->base);
  1169. return &iommu_pt->base;
  1170. }
  1171. #else
  1172. static struct kgsl_pagetable *kgsl_iommu_secure_pagetable(struct kgsl_mmu *mmu)
  1173. {
  1174. return ERR_PTR(-EPERM);
  1175. }
  1176. #endif
  1177. static struct kgsl_pagetable *kgsl_iopgtbl_pagetable(struct kgsl_mmu *mmu, u32 name)
  1178. {
  1179. struct kgsl_iommu *iommu = &mmu->iommu;
  1180. struct kgsl_iommu_pt *pt;
  1181. int ret;
  1182. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  1183. if (!pt)
  1184. return ERR_PTR(-ENOMEM);
  1185. kgsl_mmu_pagetable_init(mmu, &pt->base, name);
  1186. pt->base.fault_addr = U64_MAX;
  1187. pt->base.rbtree = RB_ROOT;
  1188. pt->base.pt_ops = &iopgtbl_pt_ops;
  1189. if (test_bit(KGSL_MMU_64BIT, &mmu->features)) {
  1190. pt->base.compat_va_start = KGSL_IOMMU_SVM_BASE32(mmu);
  1191. pt->base.compat_va_end = KGSL_IOMMU_SVM_END32(mmu);
  1192. pt->base.va_start = KGSL_IOMMU_VA_BASE64;
  1193. pt->base.va_end = KGSL_IOMMU_VA_END64;
  1194. if (is_compat_task()) {
  1195. pt->base.svm_start = KGSL_IOMMU_SVM_BASE32(mmu);
  1196. pt->base.svm_end = KGSL_IOMMU_SVM_END32(mmu);
  1197. } else {
  1198. pt->base.svm_start = KGSL_IOMMU_SVM_BASE64;
  1199. pt->base.svm_end = KGSL_IOMMU_SVM_END64;
  1200. }
  1201. } else {
  1202. pt->base.va_start = KGSL_IOMMU_SVM_BASE32(mmu);
  1203. pt->base.va_end = KGSL_IOMMU_GLOBAL_MEM_BASE(mmu);
  1204. pt->base.compat_va_start = pt->base.va_start;
  1205. pt->base.compat_va_end = pt->base.va_end;
  1206. pt->base.svm_start = KGSL_IOMMU_SVM_BASE32(mmu);
  1207. pt->base.svm_end = KGSL_IOMMU_SVM_END32(mmu);
  1208. }
  1209. /*
  1210. * We expect the 64-bit SVM and non-SVM ranges not to overlap so that
  1211. * va_hint points to VA space at the top of the 64-bit non-SVM range.
  1212. */
  1213. BUILD_BUG_ON_MSG(!((KGSL_IOMMU_VA_BASE64 >= KGSL_IOMMU_SVM_END64) ||
  1214. (KGSL_IOMMU_SVM_BASE64 >= KGSL_IOMMU_VA_END64)),
  1215. "64-bit SVM and non-SVM ranges should not overlap");
  1216. /* Set up the hint for 64-bit non-SVM VA on per-process pagetables */
  1217. pt->base.va_hint = pt->base.va_start;
  1218. ret = kgsl_iopgtbl_alloc(&iommu->user_context, pt);
  1219. if (ret) {
  1220. kfree(pt);
  1221. return ERR_PTR(ret);
  1222. }
  1223. kgsl_mmu_pagetable_add(mmu, &pt->base);
  1224. return &pt->base;
  1225. }
  1226. static struct kgsl_pagetable *kgsl_iommu_getpagetable(struct kgsl_mmu *mmu,
  1227. unsigned long name)
  1228. {
  1229. struct kgsl_pagetable *pt;
  1230. /* If we already know the pagetable, return it */
  1231. pt = kgsl_get_pagetable(name);
  1232. if (pt)
  1233. return pt;
  1234. /* If io-pgtables are not in effect, just use the default pagetable */
  1235. if (!test_bit(KGSL_MMU_IOPGTABLE, &mmu->features))
  1236. return mmu->defaultpagetable;
  1237. pt = kgsl_iopgtbl_pagetable(mmu, name);
  1238. /*
  1239. * If the io-pgtable allocation didn't work then fall back to the
  1240. * default pagetable for this cycle
  1241. */
  1242. if (!pt)
  1243. return mmu->defaultpagetable;
  1244. return pt;
  1245. }
  1246. static void kgsl_iommu_detach_context(struct kgsl_iommu_context *context)
  1247. {
  1248. if (!context->domain)
  1249. return;
  1250. iommu_detach_device(context->domain, &context->pdev->dev);
  1251. iommu_domain_free(context->domain);
  1252. context->domain = NULL;
  1253. platform_device_put(context->pdev);
  1254. context->pdev = NULL;
  1255. }
  1256. static void kgsl_iommu_close(struct kgsl_mmu *mmu)
  1257. {
  1258. struct kgsl_iommu *iommu = &mmu->iommu;
  1259. /* First put away the default pagetables */
  1260. kgsl_mmu_putpagetable(mmu->defaultpagetable);
  1261. kgsl_mmu_putpagetable(mmu->securepagetable);
  1262. /*
  1263. * Flush the workqueue to ensure pagetables are
  1264. * destroyed before proceeding further
  1265. */
  1266. flush_workqueue(kgsl_driver.workqueue);
  1267. mmu->defaultpagetable = NULL;
  1268. mmu->securepagetable = NULL;
  1269. kgsl_iommu_set_ttbr0(&iommu->lpac_context, mmu, NULL);
  1270. kgsl_iommu_set_ttbr0(&iommu->user_context, mmu, NULL);
  1271. /* Next, detach the context banks */
  1272. kgsl_iommu_detach_context(&iommu->user_context);
  1273. kgsl_iommu_detach_context(&iommu->lpac_context);
  1274. kgsl_iommu_detach_context(&iommu->secure_context);
  1275. kgsl_free_secure_page(kgsl_secure_guard_page);
  1276. kgsl_secure_guard_page = NULL;
  1277. if (kgsl_guard_page != NULL) {
  1278. __free_page(kgsl_guard_page);
  1279. kgsl_guard_page = NULL;
  1280. }
  1281. kmem_cache_destroy(addr_entry_cache);
  1282. addr_entry_cache = NULL;
  1283. }
  1284. /* Program the PRR marker and enable it in the ACTLR register */
  1285. static void _iommu_context_set_prr(struct kgsl_mmu *mmu,
  1286. struct kgsl_iommu_context *ctx)
  1287. {
  1288. struct kgsl_iommu *iommu = &mmu->iommu;
  1289. struct page *page = kgsl_vbo_zero_page;
  1290. u32 val;
  1291. if (ctx->cb_num < 0)
  1292. return;
  1293. /* Quietly return if the context doesn't have a domain */
  1294. if (!ctx->domain)
  1295. return;
  1296. if (!page)
  1297. return;
  1298. writel_relaxed(lower_32_bits(page_to_phys(page)),
  1299. iommu->regbase + KGSL_IOMMU_PRR_CFG_LADDR);
  1300. writel_relaxed(upper_32_bits(page_to_phys(page)),
  1301. iommu->regbase + KGSL_IOMMU_PRR_CFG_UADDR);
  1302. val = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_ACTLR);
  1303. val |= FIELD_PREP(KGSL_IOMMU_ACTLR_PRR_ENABLE, 1);
  1304. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_ACTLR, val);
  1305. /* Make sure all of the preceding writes have posted */
  1306. wmb();
  1307. }
  1308. static void kgsl_iommu_configure_gpu_sctlr(struct kgsl_mmu *mmu,
  1309. unsigned long pf_policy,
  1310. struct kgsl_iommu_context *ctx)
  1311. {
  1312. u32 sctlr_val;
  1313. /*
  1314. * If pagefault policy is GPUHALT_ENABLE,
  1315. * If terminate feature flag is enabled:
  1316. * 1) Program CFCFG to 0 to terminate the faulting transaction
  1317. * 2) Program HUPCF to 0 (terminate subsequent transactions
  1318. * in the presence of an outstanding fault)
  1319. * Else configure stall:
  1320. * 1) Program CFCFG to 1 to enable STALL mode
  1321. * 2) Program HUPCF to 0 (Stall subsequent
  1322. * transactions in the presence of an outstanding fault)
  1323. * else
  1324. * 1) Program CFCFG to 0 to disable STALL mode (0=Terminate)
  1325. * 2) Program HUPCF to 1 (Process subsequent transactions
  1326. * independently of any outstanding fault)
  1327. */
  1328. sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR);
  1329. if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &pf_policy)) {
  1330. if (test_bit(KGSL_MMU_PAGEFAULT_TERMINATE, &mmu->features)) {
  1331. sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
  1332. sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
  1333. } else {
  1334. sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
  1335. sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
  1336. }
  1337. } else {
  1338. sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
  1339. sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
  1340. }
  1341. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR, sctlr_val);
  1342. }
  1343. static int kgsl_iommu_start(struct kgsl_mmu *mmu)
  1344. {
  1345. struct kgsl_iommu *iommu = &mmu->iommu;
  1346. kgsl_iommu_enable_clk(mmu);
  1347. /* Set the following registers only when the MMU type is QSMMU */
  1348. if (mmu->subtype != KGSL_IOMMU_SMMU_V500) {
  1349. /* Enable hazard check from GPU_SMMU_HUM_CFG */
  1350. writel_relaxed(0x02, iommu->regbase + 0x6800);
  1351. /* Write to GPU_SMMU_DORA_ORDERING to disable reordering */
  1352. writel_relaxed(0x01, iommu->regbase + 0x64a0);
  1353. /* make sure register write committed */
  1354. wmb();
  1355. }
  1356. kgsl_iommu_configure_gpu_sctlr(mmu, mmu->pfpolicy, &iommu->user_context);
  1357. _iommu_context_set_prr(mmu, &iommu->user_context);
  1358. if (mmu->secured)
  1359. _iommu_context_set_prr(mmu, &iommu->secure_context);
  1360. if (iommu->lpac_context.domain) {
  1361. _iommu_context_set_prr(mmu, &iommu->lpac_context);
  1362. kgsl_iommu_configure_gpu_sctlr(mmu, mmu->pfpolicy, &iommu->lpac_context);
  1363. }
  1364. kgsl_iommu_disable_clk(mmu);
  1365. return 0;
  1366. }
  1367. static void kgsl_iommu_context_clear_fsr(struct kgsl_mmu *mmu, struct kgsl_iommu_context *ctx)
  1368. {
  1369. unsigned int sctlr_val;
  1370. if (ctx->stalled_on_fault) {
  1371. kgsl_iommu_enable_clk(mmu);
  1372. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_FSR, 0xffffffff);
  1373. /*
  1374. * Re-enable context fault interrupts after clearing
  1375. * FSR to prevent the interrupt from firing repeatedly
  1376. */
  1377. sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR);
  1378. sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT);
  1379. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR, sctlr_val);
  1380. /*
  1381. * Make sure the above register writes
  1382. * are not reordered across the barrier
  1383. * as we use writel_relaxed to write them
  1384. */
  1385. wmb();
  1386. kgsl_iommu_disable_clk(mmu);
  1387. ctx->stalled_on_fault = false;
  1388. }
  1389. }
  1390. static void kgsl_iommu_clear_fsr(struct kgsl_mmu *mmu)
  1391. {
  1392. struct kgsl_iommu *iommu = &mmu->iommu;
  1393. kgsl_iommu_context_clear_fsr(mmu, &iommu->user_context);
  1394. if (iommu->lpac_context.domain)
  1395. kgsl_iommu_context_clear_fsr(mmu, &iommu->lpac_context);
  1396. }
  1397. static void kgsl_iommu_context_pagefault_resume(struct kgsl_iommu *iommu, struct kgsl_iommu_context *ctx,
  1398. bool terminate)
  1399. {
  1400. u32 sctlr_val = 0;
  1401. if (!ctx->stalled_on_fault)
  1402. return;
  1403. if (!terminate)
  1404. goto clear_fsr;
  1405. sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR);
  1406. /*
  1407. * As part of recovery, GBIF halt sequence should be performed.
  1408. * In a worst case scenario, if any GPU block is generating a
  1409. * stream of un-ending faulting transactions, SMMU would enter
  1410. * stall-on-fault mode again after resuming and not let GBIF
  1411. * halt succeed. In order to avoid that situation and terminate
  1412. * those faulty transactions, set CFCFG and HUPCF to 0.
  1413. */
  1414. sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
  1415. sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
  1416. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_SCTLR, sctlr_val);
  1417. /*
  1418. * Make sure the above register write is not reordered across
  1419. * the barrier as we use writel_relaxed to write it.
  1420. */
  1421. wmb();
  1422. clear_fsr:
  1423. /*
  1424. * This will only clear fault bits in FSR. FSR.SS will still
  1425. * be set. Writing to RESUME (below) is the only way to clear
  1426. * FSR.SS bit.
  1427. */
  1428. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_FSR, 0xffffffff);
  1429. /*
  1430. * Make sure the above register write is not reordered across
  1431. * the barrier as we use writel_relaxed to write it.
  1432. */
  1433. wmb();
  1434. /*
  1435. * Write 1 to RESUME.TnR to terminate the stalled transaction.
  1436. * This will also allow the SMMU to process new transactions.
  1437. */
  1438. KGSL_IOMMU_SET_CTX_REG(ctx, KGSL_IOMMU_CTX_RESUME, 1);
  1439. /*
  1440. * Make sure the above register writes are not reordered across
  1441. * the barrier as we use writel_relaxed to write them.
  1442. */
  1443. wmb();
  1444. }
  1445. static void kgsl_iommu_pagefault_resume(struct kgsl_mmu *mmu, bool terminate)
  1446. {
  1447. struct kgsl_iommu *iommu = &mmu->iommu;
  1448. kgsl_iommu_context_pagefault_resume(iommu, &iommu->user_context, terminate);
  1449. if (iommu->lpac_context.domain)
  1450. kgsl_iommu_context_pagefault_resume(iommu, &iommu->lpac_context, terminate);
  1451. }
  1452. static u64
  1453. kgsl_iommu_get_current_ttbr0(struct kgsl_mmu *mmu, struct kgsl_context *context)
  1454. {
  1455. u64 val;
  1456. struct kgsl_iommu *iommu = &mmu->iommu;
  1457. struct kgsl_iommu_context *ctx = &iommu->user_context;
  1458. if (kgsl_context_is_lpac(context))
  1459. ctx = &iommu->lpac_context;
  1460. /*
  1461. * We cannot enable or disable the clocks in interrupt context, this
  1462. * function is called from interrupt context if there is an axi error
  1463. */
  1464. if (in_interrupt())
  1465. return 0;
  1466. if (ctx->cb_num < 0)
  1467. return 0;
  1468. kgsl_iommu_enable_clk(mmu);
  1469. val = KGSL_IOMMU_GET_CTX_REG_Q(ctx, KGSL_IOMMU_CTX_TTBR0);
  1470. kgsl_iommu_disable_clk(mmu);
  1471. return val;
  1472. }
  1473. /*
  1474. * kgsl_iommu_set_pf_policy() - Set the pagefault policy for IOMMU
  1475. * @mmu: Pointer to mmu structure
  1476. * @pf_policy: The pagefault polict to set
  1477. *
  1478. * Check if the new policy indicated by pf_policy is same as current
  1479. * policy, if same then return else set the policy
  1480. */
  1481. static int kgsl_iommu_set_pf_policy_ctxt(struct kgsl_mmu *mmu,
  1482. unsigned long pf_policy, struct kgsl_iommu_context *ctx)
  1483. {
  1484. int cur, new;
  1485. struct kgsl_iommu *iommu = &mmu->iommu;
  1486. cur = test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &mmu->pfpolicy);
  1487. new = test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &pf_policy);
  1488. if (cur == new)
  1489. return 0;
  1490. kgsl_iommu_enable_clk(mmu);
  1491. kgsl_iommu_configure_gpu_sctlr(mmu, pf_policy, &iommu->user_context);
  1492. if (iommu->lpac_context.domain)
  1493. kgsl_iommu_configure_gpu_sctlr(mmu, pf_policy, &iommu->lpac_context);
  1494. kgsl_iommu_disable_clk(mmu);
  1495. return 0;
  1496. }
  1497. static int kgsl_iommu_set_pf_policy(struct kgsl_mmu *mmu,
  1498. unsigned long pf_policy)
  1499. {
  1500. struct kgsl_iommu *iommu = &mmu->iommu;
  1501. kgsl_iommu_set_pf_policy_ctxt(mmu, pf_policy, &iommu->user_context);
  1502. if (iommu->lpac_context.domain)
  1503. kgsl_iommu_set_pf_policy_ctxt(mmu, pf_policy, &iommu->lpac_context);
  1504. return 0;
  1505. }
  1506. static struct kgsl_iommu_addr_entry *_find_gpuaddr(
  1507. struct kgsl_pagetable *pagetable, uint64_t gpuaddr)
  1508. {
  1509. struct rb_node *node = pagetable->rbtree.rb_node;
  1510. while (node != NULL) {
  1511. struct kgsl_iommu_addr_entry *entry = rb_entry(node,
  1512. struct kgsl_iommu_addr_entry, node);
  1513. if (gpuaddr < entry->base)
  1514. node = node->rb_left;
  1515. else if (gpuaddr > entry->base)
  1516. node = node->rb_right;
  1517. else
  1518. return entry;
  1519. }
  1520. return NULL;
  1521. }
  1522. static int _remove_gpuaddr(struct kgsl_pagetable *pagetable,
  1523. uint64_t gpuaddr)
  1524. {
  1525. struct kgsl_iommu_addr_entry *entry;
  1526. entry = _find_gpuaddr(pagetable, gpuaddr);
  1527. if (WARN(!entry, "GPU address %llx doesn't exist\n", gpuaddr))
  1528. return -ENOMEM;
  1529. /*
  1530. * If the hint was based on this entry, adjust it to the end of the
  1531. * previous entry.
  1532. */
  1533. if (pagetable->va_hint == (entry->base + entry->size)) {
  1534. struct kgsl_iommu_addr_entry *prev =
  1535. rb_entry_safe(rb_prev(&entry->node),
  1536. struct kgsl_iommu_addr_entry, node);
  1537. pagetable->va_hint = pagetable->va_start;
  1538. if (prev)
  1539. pagetable->va_hint = max_t(u64, prev->base + prev->size,
  1540. pagetable->va_start);
  1541. }
  1542. rb_erase(&entry->node, &pagetable->rbtree);
  1543. kmem_cache_free(addr_entry_cache, entry);
  1544. return 0;
  1545. }
  1546. static int _insert_gpuaddr(struct kgsl_pagetable *pagetable,
  1547. uint64_t gpuaddr, uint64_t size)
  1548. {
  1549. struct rb_node **node, *parent = NULL;
  1550. struct kgsl_iommu_addr_entry *new =
  1551. kmem_cache_alloc(addr_entry_cache, GFP_ATOMIC);
  1552. if (new == NULL)
  1553. return -ENOMEM;
  1554. new->base = gpuaddr;
  1555. new->size = size;
  1556. node = &pagetable->rbtree.rb_node;
  1557. while (*node != NULL) {
  1558. struct kgsl_iommu_addr_entry *this;
  1559. parent = *node;
  1560. this = rb_entry(parent, struct kgsl_iommu_addr_entry, node);
  1561. if (new->base < this->base)
  1562. node = &parent->rb_left;
  1563. else if (new->base > this->base)
  1564. node = &parent->rb_right;
  1565. else {
  1566. /* Duplicate entry */
  1567. WARN_RATELIMIT(1, "duplicate gpuaddr: 0x%llx\n", gpuaddr);
  1568. kmem_cache_free(addr_entry_cache, new);
  1569. return -EEXIST;
  1570. }
  1571. }
  1572. rb_link_node(&new->node, parent, node);
  1573. rb_insert_color(&new->node, &pagetable->rbtree);
  1574. return 0;
  1575. }
  1576. static u64 _get_unmapped_area_hint(struct kgsl_pagetable *pagetable,
  1577. u64 bottom, u64 top, u64 size, u64 align)
  1578. {
  1579. u64 hint;
  1580. /*
  1581. * VA fragmentation can be a problem on global and secure pagetables
  1582. * that are common to all processes, or if we're constrained to a 32-bit
  1583. * range. Don't use the va_hint in these cases.
  1584. */
  1585. if (!pagetable->va_hint || !upper_32_bits(top))
  1586. return (u64) -EINVAL;
  1587. /* Satisfy requested alignment */
  1588. hint = ALIGN(pagetable->va_hint, align);
  1589. /*
  1590. * The va_hint is the highest VA that was allocated in the non-SVM
  1591. * region. The 64-bit SVM and non-SVM regions do not overlap. So, we
  1592. * know there is no VA allocated at this gpuaddr. Therefore, we only
  1593. * need to check whether we have enough space for this allocation.
  1594. */
  1595. if ((hint + size) > top)
  1596. return (u64) -ENOMEM;
  1597. pagetable->va_hint = hint + size;
  1598. return hint;
  1599. }
  1600. static uint64_t _get_unmapped_area(struct kgsl_pagetable *pagetable,
  1601. uint64_t bottom, uint64_t top, uint64_t size,
  1602. uint64_t align)
  1603. {
  1604. struct rb_node *node;
  1605. uint64_t start;
  1606. /* Check if we can assign a gpuaddr based on the last allocation */
  1607. start = _get_unmapped_area_hint(pagetable, bottom, top, size, align);
  1608. if (!IS_ERR_VALUE(start))
  1609. return start;
  1610. /* Fall back to searching through the range. */
  1611. node = rb_first(&pagetable->rbtree);
  1612. bottom = ALIGN(bottom, align);
  1613. start = bottom;
  1614. while (node != NULL) {
  1615. uint64_t gap;
  1616. struct kgsl_iommu_addr_entry *entry = rb_entry(node,
  1617. struct kgsl_iommu_addr_entry, node);
  1618. /*
  1619. * Skip any entries that are outside of the range, but make sure
  1620. * to account for some that might straddle the lower bound
  1621. */
  1622. if (entry->base < bottom) {
  1623. if (entry->base + entry->size > bottom)
  1624. start = ALIGN(entry->base + entry->size, align);
  1625. node = rb_next(node);
  1626. continue;
  1627. }
  1628. /* Stop if we went over the top */
  1629. if (entry->base >= top)
  1630. break;
  1631. /* Make sure there is a gap to consider */
  1632. if (start < entry->base) {
  1633. gap = entry->base - start;
  1634. if (gap >= size)
  1635. return start;
  1636. }
  1637. /* Stop if there is no more room in the region */
  1638. if (entry->base + entry->size >= top)
  1639. return (uint64_t) -ENOMEM;
  1640. /* Start the next cycle at the end of the current entry */
  1641. start = ALIGN(entry->base + entry->size, align);
  1642. node = rb_next(node);
  1643. }
  1644. if (start + size <= top)
  1645. return start;
  1646. return (uint64_t) -ENOMEM;
  1647. }
  1648. static uint64_t _get_unmapped_area_topdown(struct kgsl_pagetable *pagetable,
  1649. uint64_t bottom, uint64_t top, uint64_t size,
  1650. uint64_t align)
  1651. {
  1652. struct rb_node *node = rb_last(&pagetable->rbtree);
  1653. uint64_t end = top;
  1654. uint64_t mask = ~(align - 1);
  1655. struct kgsl_iommu_addr_entry *entry;
  1656. /* Make sure that the bottom is correctly aligned */
  1657. bottom = ALIGN(bottom, align);
  1658. /* Make sure the requested size will fit in the range */
  1659. if (size > (top - bottom))
  1660. return -ENOMEM;
  1661. /* Walk back through the list to find the highest entry in the range */
  1662. for (node = rb_last(&pagetable->rbtree); node != NULL; node = rb_prev(node)) {
  1663. entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
  1664. if (entry->base < top)
  1665. break;
  1666. }
  1667. while (node != NULL) {
  1668. uint64_t offset;
  1669. entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
  1670. /* If the entire entry is below the range the search is over */
  1671. if ((entry->base + entry->size) < bottom)
  1672. break;
  1673. /* Get the top of the entry properly aligned */
  1674. offset = ALIGN(entry->base + entry->size, align);
  1675. /*
  1676. * Try to allocate the memory from the top of the gap,
  1677. * making sure that it fits between the top of this entry and
  1678. * the bottom of the previous one
  1679. */
  1680. if ((end > size) && (offset < end)) {
  1681. uint64_t chunk = (end - size) & mask;
  1682. if (chunk >= offset)
  1683. return chunk;
  1684. }
  1685. /*
  1686. * If we get here and the current entry is outside of the range
  1687. * then we are officially out of room
  1688. */
  1689. if (entry->base < bottom)
  1690. return (uint64_t) -ENOMEM;
  1691. /* Set the top of the gap to the current entry->base */
  1692. end = entry->base;
  1693. /* And move on to the next lower entry */
  1694. node = rb_prev(node);
  1695. }
  1696. /* If we get here then there are no more entries in the region */
  1697. if ((end > size) && (((end - size) & mask) >= bottom))
  1698. return (end - size) & mask;
  1699. return (uint64_t) -ENOMEM;
  1700. }
  1701. static uint64_t kgsl_iommu_find_svm_region(struct kgsl_pagetable *pagetable,
  1702. uint64_t start, uint64_t end, uint64_t size,
  1703. uint64_t alignment)
  1704. {
  1705. uint64_t addr;
  1706. /* Avoid black holes */
  1707. if (WARN(end <= start, "Bad search range: 0x%llx-0x%llx", start, end))
  1708. return (uint64_t) -EINVAL;
  1709. spin_lock(&pagetable->lock);
  1710. addr = _get_unmapped_area_topdown(pagetable,
  1711. start, end, size, alignment);
  1712. spin_unlock(&pagetable->lock);
  1713. return addr;
  1714. }
  1715. static bool iommu_addr_in_svm_ranges(struct kgsl_pagetable *pagetable,
  1716. u64 gpuaddr, u64 size)
  1717. {
  1718. u64 end = gpuaddr + size;
  1719. /* Make sure size is not zero and we don't wrap around */
  1720. if (end <= gpuaddr)
  1721. return false;
  1722. if ((gpuaddr >= pagetable->compat_va_start && gpuaddr < pagetable->compat_va_end) &&
  1723. (end > pagetable->compat_va_start &&
  1724. end <= pagetable->compat_va_end))
  1725. return true;
  1726. if ((gpuaddr >= pagetable->svm_start && gpuaddr < pagetable->svm_end) &&
  1727. (end > pagetable->svm_start &&
  1728. end <= pagetable->svm_end))
  1729. return true;
  1730. return false;
  1731. }
  1732. static int kgsl_iommu_set_svm_region(struct kgsl_pagetable *pagetable,
  1733. uint64_t gpuaddr, uint64_t size)
  1734. {
  1735. int ret = -ENOMEM;
  1736. struct rb_node *node;
  1737. /* Make sure the requested address doesn't fall out of SVM range */
  1738. if (!iommu_addr_in_svm_ranges(pagetable, gpuaddr, size))
  1739. return -ENOMEM;
  1740. spin_lock(&pagetable->lock);
  1741. node = pagetable->rbtree.rb_node;
  1742. while (node != NULL) {
  1743. uint64_t start, end;
  1744. struct kgsl_iommu_addr_entry *entry = rb_entry(node,
  1745. struct kgsl_iommu_addr_entry, node);
  1746. start = entry->base;
  1747. end = entry->base + entry->size;
  1748. if (gpuaddr + size <= start)
  1749. node = node->rb_left;
  1750. else if (end <= gpuaddr)
  1751. node = node->rb_right;
  1752. else
  1753. goto out;
  1754. }
  1755. ret = _insert_gpuaddr(pagetable, gpuaddr, size);
  1756. out:
  1757. spin_unlock(&pagetable->lock);
  1758. return ret;
  1759. }
  1760. static int get_gpuaddr(struct kgsl_pagetable *pagetable,
  1761. struct kgsl_memdesc *memdesc, u64 start, u64 end,
  1762. u64 size, u64 align)
  1763. {
  1764. u64 addr;
  1765. int ret;
  1766. spin_lock(&pagetable->lock);
  1767. addr = _get_unmapped_area(pagetable, start, end, size, align);
  1768. if (addr == (u64) -ENOMEM) {
  1769. spin_unlock(&pagetable->lock);
  1770. return -ENOMEM;
  1771. }
  1772. ret = _insert_gpuaddr(pagetable, addr, size);
  1773. spin_unlock(&pagetable->lock);
  1774. if (ret == 0) {
  1775. memdesc->gpuaddr = addr;
  1776. memdesc->pagetable = pagetable;
  1777. }
  1778. return ret;
  1779. }
  1780. static int kgsl_iommu_get_gpuaddr(struct kgsl_pagetable *pagetable,
  1781. struct kgsl_memdesc *memdesc)
  1782. {
  1783. int ret = 0;
  1784. u64 start, end, size, align;
  1785. if (WARN_ON(kgsl_memdesc_use_cpu_map(memdesc)))
  1786. return -EINVAL;
  1787. if (memdesc->flags & KGSL_MEMFLAGS_SECURE &&
  1788. pagetable->name != KGSL_MMU_SECURE_PT)
  1789. return -EINVAL;
  1790. size = kgsl_memdesc_footprint(memdesc);
  1791. align = max_t(uint64_t, 1 << kgsl_memdesc_get_align(memdesc),
  1792. PAGE_SIZE);
  1793. if (memdesc->flags & KGSL_MEMFLAGS_FORCE_32BIT) {
  1794. start = pagetable->compat_va_start;
  1795. end = pagetable->compat_va_end;
  1796. } else {
  1797. start = pagetable->va_start;
  1798. end = pagetable->va_end;
  1799. }
  1800. ret = get_gpuaddr(pagetable, memdesc, start, end, size, align);
  1801. /* if OOM, retry once after flushing lockless_workqueue */
  1802. if (ret == -ENOMEM) {
  1803. flush_workqueue(kgsl_driver.lockless_workqueue);
  1804. ret = get_gpuaddr(pagetable, memdesc, start, end, size, align);
  1805. }
  1806. return ret;
  1807. }
  1808. static void kgsl_iommu_put_gpuaddr(struct kgsl_memdesc *memdesc)
  1809. {
  1810. if (memdesc->pagetable == NULL)
  1811. return;
  1812. spin_lock(&memdesc->pagetable->lock);
  1813. _remove_gpuaddr(memdesc->pagetable, memdesc->gpuaddr);
  1814. spin_unlock(&memdesc->pagetable->lock);
  1815. }
  1816. static int kgsl_iommu_svm_range(struct kgsl_pagetable *pagetable,
  1817. uint64_t *lo, uint64_t *hi, uint64_t memflags)
  1818. {
  1819. bool gpu_compat = (memflags & KGSL_MEMFLAGS_FORCE_32BIT) != 0;
  1820. if (lo != NULL)
  1821. *lo = gpu_compat ? pagetable->compat_va_start : pagetable->svm_start;
  1822. if (hi != NULL)
  1823. *hi = gpu_compat ? pagetable->compat_va_end : pagetable->svm_end;
  1824. return 0;
  1825. }
  1826. static bool kgsl_iommu_addr_in_range(struct kgsl_pagetable *pagetable,
  1827. uint64_t gpuaddr, uint64_t size)
  1828. {
  1829. u64 end = gpuaddr + size;
  1830. /* Make sure we don't wrap around */
  1831. if (gpuaddr == 0 || end < gpuaddr)
  1832. return false;
  1833. if (gpuaddr >= pagetable->va_start && end <= pagetable->va_end)
  1834. return true;
  1835. if (gpuaddr >= pagetable->compat_va_start &&
  1836. end <= pagetable->compat_va_end)
  1837. return true;
  1838. if (gpuaddr >= pagetable->svm_start && end <= pagetable->svm_end)
  1839. return true;
  1840. return false;
  1841. }
  1842. static int kgsl_iommu_setup_context(struct kgsl_mmu *mmu,
  1843. struct device_node *parent,
  1844. struct kgsl_iommu_context *context, const char *name,
  1845. iommu_fault_handler_t handler)
  1846. {
  1847. struct device_node *node = of_find_node_by_name(parent, name);
  1848. struct platform_device *pdev;
  1849. struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
  1850. int ret;
  1851. if (!node)
  1852. return -ENOENT;
  1853. pdev = of_find_device_by_node(node);
  1854. ret = of_dma_configure(&pdev->dev, node, true);
  1855. of_node_put(node);
  1856. if (ret)
  1857. return ret;
  1858. context->cb_num = -1;
  1859. context->name = name;
  1860. context->kgsldev = device;
  1861. context->pdev = pdev;
  1862. ratelimit_default_init(&context->ratelimit);
  1863. /* Set the adreno_smmu priv data for the device */
  1864. dev_set_drvdata(&pdev->dev, &context->adreno_smmu);
  1865. /* Create a new context */
  1866. context->domain = iommu_domain_alloc(&platform_bus_type);
  1867. if (!context->domain) {
  1868. /*FIXME: Put back the pdev here? */
  1869. return -ENODEV;
  1870. }
  1871. _enable_gpuhtw_llc(mmu, context->domain);
  1872. ret = iommu_attach_device(context->domain, &context->pdev->dev);
  1873. if (ret) {
  1874. /* FIXME: put back the device here? */
  1875. iommu_domain_free(context->domain);
  1876. context->domain = NULL;
  1877. return ret;
  1878. }
  1879. iommu_set_fault_handler(context->domain, handler, mmu);
  1880. context->cb_num = qcom_iommu_get_context_bank_nr(context->domain);
  1881. if (context->cb_num >= 0)
  1882. return 0;
  1883. dev_err(&device->pdev->dev, "Couldn't get the context bank for %s: %d\n",
  1884. context->name, context->cb_num);
  1885. iommu_detach_device(context->domain, &context->pdev->dev);
  1886. iommu_domain_free(context->domain);
  1887. /* FIXME: put back the device here? */
  1888. context->domain = NULL;
  1889. return context->cb_num;
  1890. }
  1891. static int iommu_probe_user_context(struct kgsl_device *device,
  1892. struct device_node *node)
  1893. {
  1894. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  1895. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  1896. struct kgsl_mmu *mmu = &device->mmu;
  1897. struct kgsl_iommu_pt *pt;
  1898. int ret;
  1899. ret = kgsl_iommu_setup_context(mmu, node, &iommu->user_context,
  1900. "gfx3d_user", kgsl_iommu_default_fault_handler);
  1901. if (ret)
  1902. return ret;
  1903. /*
  1904. * It is problamatic if smmu driver does system suspend before consumer
  1905. * device (gpu). So smmu driver creates a device_link to act as a
  1906. * supplier which in turn will ensure correct order during system
  1907. * suspend. In kgsl, since we don't initialize iommu on the gpu device,
  1908. * we should create a device_link between kgsl iommu device and gpu
  1909. * device to maintain a correct suspend order between smmu device and
  1910. * gpu device.
  1911. */
  1912. if (!device_link_add(&device->pdev->dev, &iommu->user_context.pdev->dev,
  1913. DL_FLAG_AUTOREMOVE_CONSUMER))
  1914. dev_err(&iommu->user_context.pdev->dev,
  1915. "Unable to create device link to gpu device\n");
  1916. ret = kgsl_iommu_setup_context(mmu, node, &iommu->lpac_context,
  1917. "gfx3d_lpac", kgsl_iommu_lpac_fault_handler);
  1918. /* LPAC is optional, ignore setup failures in absence of LPAC feature */
  1919. if ((ret < 0) && ADRENO_FEATURE(adreno_dev, ADRENO_LPAC))
  1920. goto err;
  1921. /*
  1922. * FIXME: If adreno_smmu->cookie wasn't initialized then we can't do
  1923. * IOPGTABLE
  1924. */
  1925. /* Make the default pagetable */
  1926. mmu->defaultpagetable = kgsl_iommu_default_pagetable(mmu);
  1927. if (IS_ERR(mmu->defaultpagetable))
  1928. return PTR_ERR(mmu->defaultpagetable);
  1929. /* If IOPGTABLE isn't enabled then we are done */
  1930. if (!test_bit(KGSL_MMU_IOPGTABLE, &mmu->features))
  1931. return 0;
  1932. pt = to_iommu_pt(mmu->defaultpagetable);
  1933. /* Enable TTBR0 on the default and LPAC contexts */
  1934. kgsl_iommu_set_ttbr0(&iommu->user_context, mmu, &pt->info.cfg);
  1935. ret = kgsl_set_smmu_aperture(device, &iommu->user_context);
  1936. if (ret)
  1937. goto err;
  1938. kgsl_iommu_set_ttbr0(&iommu->lpac_context, mmu, &pt->info.cfg);
  1939. ret = kgsl_set_smmu_lpac_aperture(device, &iommu->lpac_context);
  1940. if (ret < 0) {
  1941. kgsl_iommu_detach_context(&iommu->lpac_context);
  1942. goto err;
  1943. }
  1944. return 0;
  1945. err:
  1946. kgsl_mmu_putpagetable(mmu->defaultpagetable);
  1947. mmu->defaultpagetable = NULL;
  1948. kgsl_iommu_detach_context(&iommu->user_context);
  1949. return ret;
  1950. }
  1951. static int iommu_probe_secure_context(struct kgsl_device *device,
  1952. struct device_node *parent)
  1953. {
  1954. struct device_node *node;
  1955. struct platform_device *pdev;
  1956. int ret;
  1957. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  1958. struct kgsl_mmu *mmu = &device->mmu;
  1959. struct kgsl_iommu_context *context = &iommu->secure_context;
  1960. int secure_vmid = VMID_CP_PIXEL;
  1961. if (!mmu->secured)
  1962. return -EPERM;
  1963. node = of_find_node_by_name(parent, "gfx3d_secure");
  1964. if (!node)
  1965. return -ENOENT;
  1966. pdev = of_find_device_by_node(node);
  1967. ret = of_dma_configure(&pdev->dev, node, true);
  1968. of_node_put(node);
  1969. if (ret)
  1970. return ret;
  1971. context->cb_num = -1;
  1972. context->name = "gfx3d_secure";
  1973. context->kgsldev = device;
  1974. context->pdev = pdev;
  1975. ratelimit_default_init(&context->ratelimit);
  1976. context->domain = iommu_domain_alloc(&platform_bus_type);
  1977. if (!context->domain) {
  1978. /* FIXME: put away the device */
  1979. return -ENODEV;
  1980. }
  1981. ret = qcom_iommu_set_secure_vmid(context->domain, secure_vmid);
  1982. if (ret) {
  1983. dev_err(&device->pdev->dev, "Unable to set the secure VMID: %d\n", ret);
  1984. iommu_domain_free(context->domain);
  1985. context->domain = NULL;
  1986. /* FIXME: put away the device */
  1987. return ret;
  1988. }
  1989. _enable_gpuhtw_llc(mmu, context->domain);
  1990. ret = iommu_attach_device(context->domain, &context->pdev->dev);
  1991. if (ret) {
  1992. iommu_domain_free(context->domain);
  1993. /* FIXME: Put way the device */
  1994. context->domain = NULL;
  1995. return ret;
  1996. }
  1997. iommu_set_fault_handler(context->domain,
  1998. kgsl_iommu_secure_fault_handler, mmu);
  1999. context->cb_num = qcom_iommu_get_context_bank_nr(context->domain);
  2000. if (context->cb_num < 0) {
  2001. iommu_detach_device(context->domain, &context->pdev->dev);
  2002. iommu_domain_free(context->domain);
  2003. context->domain = NULL;
  2004. return context->cb_num;
  2005. }
  2006. mmu->securepagetable = kgsl_iommu_secure_pagetable(mmu);
  2007. if (IS_ERR(mmu->securepagetable))
  2008. mmu->secured = false;
  2009. return 0;
  2010. }
  2011. static const char * const kgsl_iommu_clocks[] = {
  2012. "gcc_gpu_memnoc_gfx",
  2013. "gcc_gpu_snoc_dvm_gfx",
  2014. "gpu_cc_ahb",
  2015. "gpu_cc_cx_gmu",
  2016. "gpu_cc_hlos1_vote_gpu_smmu",
  2017. "gpu_cc_hub_aon",
  2018. "gpu_cc_hub_cx_int",
  2019. "gcc_bimc_gpu_axi",
  2020. "gcc_gpu_ahb",
  2021. "gcc_gpu_axi_clk",
  2022. "gcc_smmu_cfg_clk",
  2023. "gcc_gfx_tcu_clk",
  2024. };
  2025. static const struct kgsl_mmu_ops kgsl_iommu_ops;
  2026. static void kgsl_iommu_check_config(struct kgsl_mmu *mmu,
  2027. struct device_node *parent)
  2028. {
  2029. struct device_node *node = of_find_node_by_name(parent, "gfx3d_user");
  2030. struct device_node *phandle;
  2031. if (!node)
  2032. return;
  2033. phandle = of_parse_phandle(node, "iommus", 0);
  2034. if (phandle) {
  2035. if (of_device_is_compatible(phandle, "qcom,qsmmu-v500"))
  2036. mmu->subtype = KGSL_IOMMU_SMMU_V500;
  2037. if (of_device_is_compatible(phandle, "qcom,adreno-smmu"))
  2038. set_bit(KGSL_MMU_IOPGTABLE, &mmu->features);
  2039. of_node_put(phandle);
  2040. }
  2041. of_node_put(node);
  2042. }
  2043. int kgsl_iommu_bind(struct kgsl_device *device, struct platform_device *pdev)
  2044. {
  2045. u32 val[2];
  2046. int ret, i;
  2047. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  2048. struct kgsl_mmu *mmu = &device->mmu;
  2049. struct device_node *node = pdev->dev.of_node;
  2050. struct kgsl_global_memdesc *md;
  2051. /* Create a kmem cache for the pagetable address objects */
  2052. if (!addr_entry_cache) {
  2053. addr_entry_cache = KMEM_CACHE(kgsl_iommu_addr_entry, 0);
  2054. if (!addr_entry_cache) {
  2055. ret = -ENOMEM;
  2056. goto err;
  2057. }
  2058. }
  2059. ret = of_property_read_u32_array(node, "reg", val, 2);
  2060. if (ret) {
  2061. dev_err(&device->pdev->dev,
  2062. "%pOF: Unable to read KGSL IOMMU register range\n",
  2063. node);
  2064. goto err;
  2065. }
  2066. iommu->regbase = devm_ioremap(&device->pdev->dev, val[0], val[1]);
  2067. if (!iommu->regbase) {
  2068. dev_err(&device->pdev->dev, "Couldn't map IOMMU registers\n");
  2069. ret = -ENOMEM;
  2070. goto err;
  2071. }
  2072. iommu->pdev = pdev;
  2073. iommu->num_clks = 0;
  2074. iommu->clks = devm_kcalloc(&pdev->dev, ARRAY_SIZE(kgsl_iommu_clocks),
  2075. sizeof(*iommu->clks), GFP_KERNEL);
  2076. if (!iommu->clks) {
  2077. ret = -ENOMEM;
  2078. goto err;
  2079. }
  2080. for (i = 0; i < ARRAY_SIZE(kgsl_iommu_clocks); i++) {
  2081. struct clk *c;
  2082. c = devm_clk_get(&device->pdev->dev, kgsl_iommu_clocks[i]);
  2083. if (IS_ERR(c))
  2084. continue;
  2085. iommu->clks[iommu->num_clks].id = kgsl_iommu_clocks[i];
  2086. iommu->clks[iommu->num_clks++].clk = c;
  2087. }
  2088. /* Get the CX regulator if it is available */
  2089. iommu->cx_gdsc = devm_regulator_get(&pdev->dev, "vddcx");
  2090. set_bit(KGSL_MMU_PAGED, &mmu->features);
  2091. mmu->type = KGSL_MMU_TYPE_IOMMU;
  2092. mmu->mmu_ops = &kgsl_iommu_ops;
  2093. /* Peek at the phandle to set up configuration */
  2094. kgsl_iommu_check_config(mmu, node);
  2095. /* Probe the default pagetable */
  2096. ret = iommu_probe_user_context(device, node);
  2097. if (ret)
  2098. goto err;
  2099. /* Probe the secure pagetable (this is optional) */
  2100. iommu_probe_secure_context(device, node);
  2101. /* Map any globals that might have been created early */
  2102. list_for_each_entry(md, &device->globals, node) {
  2103. if (md->memdesc.flags & KGSL_MEMFLAGS_SECURE) {
  2104. if (IS_ERR_OR_NULL(mmu->securepagetable))
  2105. continue;
  2106. kgsl_iommu_secure_map(mmu->securepagetable,
  2107. &md->memdesc);
  2108. } else
  2109. kgsl_iommu_default_map(mmu->defaultpagetable,
  2110. &md->memdesc);
  2111. }
  2112. /* QDSS is supported only when QCOM_KGSL_QDSS_STM is enabled */
  2113. if (IS_ENABLED(CONFIG_QCOM_KGSL_QDSS_STM))
  2114. device->qdss_desc = kgsl_allocate_global_fixed(device,
  2115. "qcom,gpu-qdss-stm", "gpu-qdss");
  2116. device->qtimer_desc = kgsl_allocate_global_fixed(device,
  2117. "qcom,gpu-timer", "gpu-qtimer");
  2118. /*
  2119. * Only support VBOs on MMU500 hardware that supports the PRR
  2120. * marker register to ignore writes to the zero page
  2121. */
  2122. if ((mmu->subtype == KGSL_IOMMU_SMMU_V500) &&
  2123. test_bit(KGSL_MMU_SUPPORT_VBO, &mmu->features)) {
  2124. /*
  2125. * We need to allocate a page because we need a known physical
  2126. * address to program in the PRR register but the hardware
  2127. * should intercept accesses to the page before they go to DDR
  2128. * so this should be mostly just a placeholder
  2129. */
  2130. kgsl_vbo_zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
  2131. __GFP_NORETRY | __GFP_HIGHMEM);
  2132. }
  2133. if (!kgsl_vbo_zero_page)
  2134. clear_bit(KGSL_MMU_SUPPORT_VBO, &mmu->features);
  2135. return 0;
  2136. err:
  2137. kmem_cache_destroy(addr_entry_cache);
  2138. addr_entry_cache = NULL;
  2139. return ret;
  2140. }
  2141. static const struct kgsl_mmu_ops kgsl_iommu_ops = {
  2142. .mmu_close = kgsl_iommu_close,
  2143. .mmu_start = kgsl_iommu_start,
  2144. .mmu_clear_fsr = kgsl_iommu_clear_fsr,
  2145. .mmu_get_current_ttbr0 = kgsl_iommu_get_current_ttbr0,
  2146. .mmu_enable_clk = kgsl_iommu_enable_clk,
  2147. .mmu_disable_clk = kgsl_iommu_disable_clk,
  2148. .mmu_set_pf_policy = kgsl_iommu_set_pf_policy,
  2149. .mmu_pagefault_resume = kgsl_iommu_pagefault_resume,
  2150. .mmu_getpagetable = kgsl_iommu_getpagetable,
  2151. .mmu_map_global = kgsl_iommu_map_global,
  2152. .mmu_send_tlb_hint = kgsl_iommu_send_tlb_hint,
  2153. };
  2154. static const struct kgsl_mmu_pt_ops iopgtbl_pt_ops = {
  2155. .mmu_map = kgsl_iopgtbl_map,
  2156. .mmu_map_child = kgsl_iopgtbl_map_child,
  2157. .mmu_map_zero_page_to_range = kgsl_iopgtbl_map_zero_page_to_range,
  2158. .mmu_unmap = kgsl_iopgtbl_unmap,
  2159. .mmu_unmap_range = kgsl_iopgtbl_unmap_range,
  2160. .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable,
  2161. .get_ttbr0 = kgsl_iommu_get_ttbr0,
  2162. .get_context_bank = kgsl_iommu_get_context_bank,
  2163. .get_asid = kgsl_iommu_get_asid,
  2164. .get_gpuaddr = kgsl_iommu_get_gpuaddr,
  2165. .put_gpuaddr = kgsl_iommu_put_gpuaddr,
  2166. .set_svm_region = kgsl_iommu_set_svm_region,
  2167. .find_svm_region = kgsl_iommu_find_svm_region,
  2168. .svm_range = kgsl_iommu_svm_range,
  2169. .addr_in_range = kgsl_iommu_addr_in_range,
  2170. };
  2171. static const struct kgsl_mmu_pt_ops secure_pt_ops = {
  2172. .mmu_map = kgsl_iommu_secure_map,
  2173. .mmu_unmap = kgsl_iommu_secure_unmap,
  2174. .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable,
  2175. .get_context_bank = kgsl_iommu_get_context_bank,
  2176. .get_asid = kgsl_iommu_get_asid,
  2177. .get_gpuaddr = kgsl_iommu_get_gpuaddr,
  2178. .put_gpuaddr = kgsl_iommu_put_gpuaddr,
  2179. .addr_in_range = kgsl_iommu_addr_in_range,
  2180. };
  2181. static const struct kgsl_mmu_pt_ops default_pt_ops = {
  2182. .mmu_map = kgsl_iommu_default_map,
  2183. .mmu_unmap = kgsl_iommu_default_unmap,
  2184. .mmu_destroy_pagetable = kgsl_iommu_destroy_default_pagetable,
  2185. .get_ttbr0 = kgsl_iommu_get_ttbr0,
  2186. .get_context_bank = kgsl_iommu_get_context_bank,
  2187. .get_asid = kgsl_iommu_get_asid,
  2188. .get_gpuaddr = kgsl_iommu_get_gpuaddr,
  2189. .put_gpuaddr = kgsl_iommu_put_gpuaddr,
  2190. .addr_in_range = kgsl_iommu_addr_in_range,
  2191. };