pvr_mmu.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /* Copyright (c) 2023 Imagination Technologies Ltd. */
  3. #include "pvr_mmu.h"
  4. #include "pvr_ccb.h"
  5. #include "pvr_device.h"
  6. #include "pvr_fw.h"
  7. #include "pvr_gem.h"
  8. #include "pvr_power.h"
  9. #include "pvr_rogue_fwif.h"
  10. #include "pvr_rogue_mmu_defs.h"
  11. #include <drm/drm_drv.h>
  12. #include <linux/atomic.h>
  13. #include <linux/bitops.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/kmemleak.h>
  16. #include <linux/minmax.h>
  17. #include <linux/sizes.h>
  18. #define PVR_SHIFT_FROM_SIZE(size_) (__builtin_ctzll(size_))
  19. #define PVR_MASK_FROM_SIZE(size_) (~((size_) - U64_C(1)))
  20. /*
  21. * The value of the device page size (%PVR_DEVICE_PAGE_SIZE) is currently
  22. * pegged to the host page size (%PAGE_SIZE). This chunk of macro goodness both
  23. * ensures that the selected host page size corresponds to a valid device page
  24. * size and sets up values needed by the MMU code below.
  25. */
  26. #if (PVR_DEVICE_PAGE_SIZE == SZ_4K)
  27. # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_4KB
  28. # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_4KB_RANGE_SHIFT
  29. # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_4KB_RANGE_CLRMSK
  30. #elif (PVR_DEVICE_PAGE_SIZE == SZ_16K)
  31. # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_16KB
  32. # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_16KB_RANGE_SHIFT
  33. # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_16KB_RANGE_CLRMSK
  34. #elif (PVR_DEVICE_PAGE_SIZE == SZ_64K)
  35. # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_64KB
  36. # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_64KB_RANGE_SHIFT
  37. # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_64KB_RANGE_CLRMSK
  38. #elif (PVR_DEVICE_PAGE_SIZE == SZ_256K)
  39. # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_256KB
  40. # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_256KB_RANGE_SHIFT
  41. # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_256KB_RANGE_CLRMSK
  42. #elif (PVR_DEVICE_PAGE_SIZE == SZ_1M)
  43. # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_1MB
  44. # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_1MB_RANGE_SHIFT
  45. # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_1MB_RANGE_CLRMSK
  46. #elif (PVR_DEVICE_PAGE_SIZE == SZ_2M)
  47. # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_2MB
  48. # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_2MB_RANGE_SHIFT
  49. # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_2MB_RANGE_CLRMSK
  50. #else
  51. # error Unsupported device page size PVR_DEVICE_PAGE_SIZE
  52. #endif
  53. #define ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X \
  54. (ROGUE_MMUCTRL_ENTRIES_PT_VALUE >> \
  55. (PVR_DEVICE_PAGE_SHIFT - PVR_SHIFT_FROM_SIZE(SZ_4K)))
  56. enum pvr_mmu_sync_level {
  57. PVR_MMU_SYNC_LEVEL_NONE = -1,
  58. PVR_MMU_SYNC_LEVEL_0 = 0,
  59. PVR_MMU_SYNC_LEVEL_1 = 1,
  60. PVR_MMU_SYNC_LEVEL_2 = 2,
  61. };
  62. #define PVR_MMU_SYNC_LEVEL_0_FLAGS (ROGUE_FWIF_MMUCACHEDATA_FLAGS_PT | \
  63. ROGUE_FWIF_MMUCACHEDATA_FLAGS_INTERRUPT | \
  64. ROGUE_FWIF_MMUCACHEDATA_FLAGS_TLB)
  65. #define PVR_MMU_SYNC_LEVEL_1_FLAGS (PVR_MMU_SYNC_LEVEL_0_FLAGS | ROGUE_FWIF_MMUCACHEDATA_FLAGS_PD)
  66. #define PVR_MMU_SYNC_LEVEL_2_FLAGS (PVR_MMU_SYNC_LEVEL_1_FLAGS | ROGUE_FWIF_MMUCACHEDATA_FLAGS_PC)
  67. /**
  68. * pvr_mmu_set_flush_flags() - Set MMU cache flush flags for next call to
  69. * pvr_mmu_flush_exec().
  70. * @pvr_dev: Target PowerVR device.
  71. * @flags: MMU flush flags. Must be one of %PVR_MMU_SYNC_LEVEL_*_FLAGS.
  72. *
  73. * This function must be called following any possible change to the MMU page
  74. * tables.
  75. */
  76. static void pvr_mmu_set_flush_flags(struct pvr_device *pvr_dev, u32 flags)
  77. {
  78. atomic_fetch_or(flags, &pvr_dev->mmu_flush_cache_flags);
  79. }
  80. /**
  81. * pvr_mmu_flush_request_all() - Request flush of all MMU caches when
  82. * subsequently calling pvr_mmu_flush_exec().
  83. * @pvr_dev: Target PowerVR device.
  84. *
  85. * This function must be called following any possible change to the MMU page
  86. * tables.
  87. */
  88. void pvr_mmu_flush_request_all(struct pvr_device *pvr_dev)
  89. {
  90. pvr_mmu_set_flush_flags(pvr_dev, PVR_MMU_SYNC_LEVEL_2_FLAGS);
  91. }
  92. /**
  93. * pvr_mmu_flush_exec() - Execute a flush of all MMU caches previously
  94. * requested.
  95. * @pvr_dev: Target PowerVR device.
  96. * @wait: Do not return until the flush is completed.
  97. *
  98. * This function must be called prior to submitting any new GPU job. The flush
  99. * will complete before the jobs are scheduled, so this can be called once after
  100. * a series of maps. However, a single unmap should always be immediately
  101. * followed by a flush and it should be explicitly waited by setting @wait.
  102. *
  103. * As a failure to flush the MMU caches could risk memory corruption, if the
  104. * flush fails (implying the firmware is not responding) then the GPU device is
  105. * marked as lost.
  106. *
  107. * Returns:
  108. * * 0 on success when @wait is true, or
  109. * * -%EIO if the device is unavailable, or
  110. * * Any error encountered while submitting the flush command via the KCCB.
  111. */
  112. int pvr_mmu_flush_exec(struct pvr_device *pvr_dev, bool wait)
  113. {
  114. struct rogue_fwif_kccb_cmd cmd_mmu_cache = {};
  115. struct rogue_fwif_mmucachedata *cmd_mmu_cache_data =
  116. &cmd_mmu_cache.cmd_data.mmu_cache_data;
  117. int err = 0;
  118. u32 slot;
  119. int idx;
  120. if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx))
  121. return -EIO;
  122. /* Can't flush MMU if the firmware hasn't booted yet. */
  123. if (!pvr_dev->fw_dev.booted)
  124. goto err_drm_dev_exit;
  125. cmd_mmu_cache_data->cache_flags =
  126. atomic_xchg(&pvr_dev->mmu_flush_cache_flags, 0);
  127. if (!cmd_mmu_cache_data->cache_flags)
  128. goto err_drm_dev_exit;
  129. cmd_mmu_cache.cmd_type = ROGUE_FWIF_KCCB_CMD_MMUCACHE;
  130. pvr_fw_object_get_fw_addr(pvr_dev->fw_dev.mem.mmucache_sync_obj,
  131. &cmd_mmu_cache_data->mmu_cache_sync_fw_addr);
  132. cmd_mmu_cache_data->mmu_cache_sync_update_value = 0;
  133. err = pvr_kccb_send_cmd(pvr_dev, &cmd_mmu_cache, &slot);
  134. if (err)
  135. goto err_reset_and_retry;
  136. err = pvr_kccb_wait_for_completion(pvr_dev, slot, HZ, NULL);
  137. if (err)
  138. goto err_reset_and_retry;
  139. drm_dev_exit(idx);
  140. return 0;
  141. err_reset_and_retry:
  142. /*
  143. * Flush command failure is most likely the result of a firmware lockup. Hard
  144. * reset the GPU and retry.
  145. */
  146. err = pvr_power_reset(pvr_dev, true);
  147. if (err)
  148. goto err_drm_dev_exit; /* Device is lost. */
  149. /* Retry sending flush request. */
  150. err = pvr_kccb_send_cmd(pvr_dev, &cmd_mmu_cache, &slot);
  151. if (err) {
  152. pvr_device_lost(pvr_dev);
  153. goto err_drm_dev_exit;
  154. }
  155. if (wait) {
  156. err = pvr_kccb_wait_for_completion(pvr_dev, slot, HZ, NULL);
  157. if (err)
  158. pvr_device_lost(pvr_dev);
  159. }
  160. err_drm_dev_exit:
  161. drm_dev_exit(idx);
  162. return err;
  163. }
  164. /**
  165. * DOC: PowerVR Virtual Memory Handling
  166. */
  167. /**
  168. * DOC: PowerVR Virtual Memory Handling (constants)
  169. *
  170. * .. c:macro:: PVR_IDX_INVALID
  171. *
  172. * Default value for a u16-based index.
  173. *
  174. * This value cannot be zero, since zero is a valid index value.
  175. */
  176. #define PVR_IDX_INVALID ((u16)(-1))
  177. /**
  178. * DOC: MMU backing pages
  179. */
  180. /**
  181. * DOC: MMU backing pages (constants)
  182. *
  183. * .. c:macro:: PVR_MMU_BACKING_PAGE_SIZE
  184. *
  185. * Page size of a PowerVR device's integrated MMU. The CPU page size must be
  186. * at least as large as this value for the current implementation; this is
  187. * checked at compile-time.
  188. */
  189. #define PVR_MMU_BACKING_PAGE_SIZE SZ_4K
  190. static_assert(PAGE_SIZE >= PVR_MMU_BACKING_PAGE_SIZE);
  191. /**
  192. * struct pvr_mmu_backing_page - Represents a single page used to back a page
  193. * table of any level.
  194. * @dma_addr: DMA address of this page.
  195. * @host_ptr: CPU address of this page.
  196. * @pvr_dev: The PowerVR device to which this page is associated. **For
  197. * internal use only.**
  198. */
  199. struct pvr_mmu_backing_page {
  200. dma_addr_t dma_addr;
  201. void *host_ptr;
  202. /* private: internal use only */
  203. struct page *raw_page;
  204. struct pvr_device *pvr_dev;
  205. };
  206. /**
  207. * pvr_mmu_backing_page_init() - Initialize a MMU backing page.
  208. * @page: Target backing page.
  209. * @pvr_dev: Target PowerVR device.
  210. *
  211. * This function performs three distinct operations:
  212. *
  213. * 1. Allocate a single page,
  214. * 2. Map the page to the CPU, and
  215. * 3. Map the page to DMA-space.
  216. *
  217. * It is expected that @page be zeroed (e.g. from kzalloc()) before calling
  218. * this function.
  219. *
  220. * Return:
  221. * * 0 on success, or
  222. * * -%ENOMEM if allocation of the backing page or mapping of the backing
  223. * page to DMA fails.
  224. */
  225. static int
  226. pvr_mmu_backing_page_init(struct pvr_mmu_backing_page *page,
  227. struct pvr_device *pvr_dev)
  228. {
  229. struct device *dev = from_pvr_device(pvr_dev)->dev;
  230. struct page *raw_page;
  231. int err;
  232. dma_addr_t dma_addr;
  233. void *host_ptr;
  234. raw_page = alloc_page(__GFP_ZERO | GFP_KERNEL);
  235. if (!raw_page)
  236. return -ENOMEM;
  237. host_ptr = vmap(&raw_page, 1, VM_MAP, pgprot_writecombine(PAGE_KERNEL));
  238. if (!host_ptr) {
  239. err = -ENOMEM;
  240. goto err_free_page;
  241. }
  242. dma_addr = dma_map_page(dev, raw_page, 0, PVR_MMU_BACKING_PAGE_SIZE,
  243. DMA_TO_DEVICE);
  244. if (dma_mapping_error(dev, dma_addr)) {
  245. err = -ENOMEM;
  246. goto err_unmap_page;
  247. }
  248. page->dma_addr = dma_addr;
  249. page->host_ptr = host_ptr;
  250. page->pvr_dev = pvr_dev;
  251. page->raw_page = raw_page;
  252. kmemleak_alloc(page->host_ptr, PAGE_SIZE, 1, GFP_KERNEL);
  253. return 0;
  254. err_unmap_page:
  255. vunmap(host_ptr);
  256. err_free_page:
  257. __free_page(raw_page);
  258. return err;
  259. }
  260. /**
  261. * pvr_mmu_backing_page_fini() - Teardown a MMU backing page.
  262. * @page: Target backing page.
  263. *
  264. * This function performs the mirror operations to pvr_mmu_backing_page_init(),
  265. * in reverse order:
  266. *
  267. * 1. Unmap the page from DMA-space,
  268. * 2. Unmap the page from the CPU, and
  269. * 3. Free the page.
  270. *
  271. * It also zeros @page.
  272. *
  273. * It is a no-op to call this function a second (or further) time on any @page.
  274. */
  275. static void
  276. pvr_mmu_backing_page_fini(struct pvr_mmu_backing_page *page)
  277. {
  278. struct device *dev;
  279. /* Do nothing if no allocation is present. */
  280. if (!page->pvr_dev)
  281. return;
  282. dev = from_pvr_device(page->pvr_dev)->dev;
  283. dma_unmap_page(dev, page->dma_addr, PVR_MMU_BACKING_PAGE_SIZE,
  284. DMA_TO_DEVICE);
  285. kmemleak_free(page->host_ptr);
  286. vunmap(page->host_ptr);
  287. __free_page(page->raw_page);
  288. memset(page, 0, sizeof(*page));
  289. }
  290. /**
  291. * pvr_mmu_backing_page_sync() - Flush a MMU backing page from the CPU to the
  292. * device.
  293. * @page: Target backing page.
  294. * @flags: MMU flush flags. Must be one of %PVR_MMU_SYNC_LEVEL_*_FLAGS.
  295. *
  296. * .. caution::
  297. *
  298. * **This is potentially an expensive function call.** Only call
  299. * pvr_mmu_backing_page_sync() once you're sure you have no more changes to
  300. * make to the backing page in the immediate future.
  301. */
  302. static void
  303. pvr_mmu_backing_page_sync(struct pvr_mmu_backing_page *page, u32 flags)
  304. {
  305. struct pvr_device *pvr_dev = page->pvr_dev;
  306. struct device *dev;
  307. /*
  308. * Do nothing if no allocation is present. This may be the case if
  309. * we are unmapping pages.
  310. */
  311. if (!pvr_dev)
  312. return;
  313. dev = from_pvr_device(pvr_dev)->dev;
  314. dma_sync_single_for_device(dev, page->dma_addr,
  315. PVR_MMU_BACKING_PAGE_SIZE, DMA_TO_DEVICE);
  316. pvr_mmu_set_flush_flags(pvr_dev, flags);
  317. }
  318. /**
  319. * DOC: Raw page tables
  320. */
  321. #define PVR_PAGE_TABLE_TYPEOF_ENTRY(level_) \
  322. typeof_member(struct pvr_page_table_l##level_##_entry_raw, val)
  323. #define PVR_PAGE_TABLE_FIELD_GET(level_, name_, field_, entry_) \
  324. (((entry_).val & \
  325. ~ROGUE_MMUCTRL_##name_##_DATA_##field_##_CLRMSK) >> \
  326. ROGUE_MMUCTRL_##name_##_DATA_##field_##_SHIFT)
  327. #define PVR_PAGE_TABLE_FIELD_PREP(level_, name_, field_, val_) \
  328. ((((PVR_PAGE_TABLE_TYPEOF_ENTRY(level_))(val_)) \
  329. << ROGUE_MMUCTRL_##name_##_DATA_##field_##_SHIFT) & \
  330. ~ROGUE_MMUCTRL_##name_##_DATA_##field_##_CLRMSK)
  331. /**
  332. * struct pvr_page_table_l2_entry_raw - A single entry in a level 2 page table.
  333. * @val: The raw value of this entry.
  334. *
  335. * This type is a structure for type-checking purposes. At compile-time, its
  336. * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PC_VALUE.
  337. *
  338. * The value stored in this structure can be decoded using the following bitmap:
  339. *
  340. * .. flat-table::
  341. * :widths: 1 5
  342. * :stub-columns: 1
  343. *
  344. * * - 31..4
  345. * - **Level 1 Page Table Base Address:** Bits 39..12 of the L1
  346. * page table base address, which is 4KiB aligned.
  347. *
  348. * * - 3..2
  349. * - *(reserved)*
  350. *
  351. * * - 1
  352. * - **Pending:** When valid bit is not set, indicates that a valid
  353. * entry is pending and the MMU should wait for the driver to map
  354. * the entry. This is used to support page demand mapping of
  355. * memory.
  356. *
  357. * * - 0
  358. * - **Valid:** Indicates that the entry contains a valid L1 page
  359. * table. If the valid bit is not set, then an attempted use of
  360. * the page would result in a page fault.
  361. */
  362. struct pvr_page_table_l2_entry_raw {
  363. u32 val;
  364. } __packed;
  365. static_assert(sizeof(struct pvr_page_table_l2_entry_raw) * 8 ==
  366. ROGUE_MMUCTRL_ENTRY_SIZE_PC_VALUE);
  367. static bool
  368. pvr_page_table_l2_entry_raw_is_valid(struct pvr_page_table_l2_entry_raw entry)
  369. {
  370. return PVR_PAGE_TABLE_FIELD_GET(2, PC, VALID, entry);
  371. }
  372. /**
  373. * pvr_page_table_l2_entry_raw_set() - Write a valid entry into a raw level 2
  374. * page table.
  375. * @entry: Target raw level 2 page table entry.
  376. * @child_table_dma_addr: DMA address of the level 1 page table to be
  377. * associated with @entry.
  378. *
  379. * When calling this function, @child_table_dma_addr must be a valid DMA
  380. * address and a multiple of %ROGUE_MMUCTRL_PC_DATA_PD_BASE_ALIGNSIZE.
  381. */
  382. static void
  383. pvr_page_table_l2_entry_raw_set(struct pvr_page_table_l2_entry_raw *entry,
  384. dma_addr_t child_table_dma_addr)
  385. {
  386. child_table_dma_addr >>= ROGUE_MMUCTRL_PC_DATA_PD_BASE_ALIGNSHIFT;
  387. WRITE_ONCE(entry->val,
  388. PVR_PAGE_TABLE_FIELD_PREP(2, PC, VALID, true) |
  389. PVR_PAGE_TABLE_FIELD_PREP(2, PC, ENTRY_PENDING, false) |
  390. PVR_PAGE_TABLE_FIELD_PREP(2, PC, PD_BASE, child_table_dma_addr));
  391. }
  392. static void
  393. pvr_page_table_l2_entry_raw_clear(struct pvr_page_table_l2_entry_raw *entry)
  394. {
  395. WRITE_ONCE(entry->val, 0);
  396. }
  397. /**
  398. * struct pvr_page_table_l1_entry_raw - A single entry in a level 1 page table.
  399. * @val: The raw value of this entry.
  400. *
  401. * This type is a structure for type-checking purposes. At compile-time, its
  402. * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PD_VALUE.
  403. *
  404. * The value stored in this structure can be decoded using the following bitmap:
  405. *
  406. * .. flat-table::
  407. * :widths: 1 5
  408. * :stub-columns: 1
  409. *
  410. * * - 63..41
  411. * - *(reserved)*
  412. *
  413. * * - 40
  414. * - **Pending:** When valid bit is not set, indicates that a valid entry
  415. * is pending and the MMU should wait for the driver to map the entry.
  416. * This is used to support page demand mapping of memory.
  417. *
  418. * * - 39..5
  419. * - **Level 0 Page Table Base Address:** The way this value is
  420. * interpreted depends on the page size. Bits not specified in the
  421. * table below (e.g. bits 11..5 for page size 4KiB) should be
  422. * considered reserved.
  423. *
  424. * This table shows the bits used in an L1 page table entry to
  425. * represent the Physical Table Base Address for a given Page Size.
  426. * Since each L1 page table entry covers 2MiB of address space, the
  427. * maximum page size is 2MiB.
  428. *
  429. * .. flat-table::
  430. * :widths: 1 1 1 1
  431. * :header-rows: 1
  432. * :stub-columns: 1
  433. *
  434. * * - Page size
  435. * - L0 page table base address bits
  436. * - Number of L0 page table entries
  437. * - Size of L0 page table
  438. *
  439. * * - 4KiB
  440. * - 39..12
  441. * - 512
  442. * - 4KiB
  443. *
  444. * * - 16KiB
  445. * - 39..10
  446. * - 128
  447. * - 1KiB
  448. *
  449. * * - 64KiB
  450. * - 39..8
  451. * - 32
  452. * - 256B
  453. *
  454. * * - 256KiB
  455. * - 39..6
  456. * - 8
  457. * - 64B
  458. *
  459. * * - 1MiB
  460. * - 39..5 (4 = '0')
  461. * - 2
  462. * - 16B
  463. *
  464. * * - 2MiB
  465. * - 39..5 (4..3 = '00')
  466. * - 1
  467. * - 8B
  468. *
  469. * * - 4
  470. * - *(reserved)*
  471. *
  472. * * - 3..1
  473. * - **Page Size:** Sets the page size, from 4KiB to 2MiB.
  474. *
  475. * * - 0
  476. * - **Valid:** Indicates that the entry contains a valid L0 page table.
  477. * If the valid bit is not set, then an attempted use of the page would
  478. * result in a page fault.
  479. */
  480. struct pvr_page_table_l1_entry_raw {
  481. u64 val;
  482. } __packed;
  483. static_assert(sizeof(struct pvr_page_table_l1_entry_raw) * 8 ==
  484. ROGUE_MMUCTRL_ENTRY_SIZE_PD_VALUE);
  485. static bool
  486. pvr_page_table_l1_entry_raw_is_valid(struct pvr_page_table_l1_entry_raw entry)
  487. {
  488. return PVR_PAGE_TABLE_FIELD_GET(1, PD, VALID, entry);
  489. }
  490. /**
  491. * pvr_page_table_l1_entry_raw_set() - Write a valid entry into a raw level 1
  492. * page table.
  493. * @entry: Target raw level 1 page table entry.
  494. * @child_table_dma_addr: DMA address of the level 0 page table to be
  495. * associated with @entry.
  496. *
  497. * When calling this function, @child_table_dma_addr must be a valid DMA
  498. * address and a multiple of 4 KiB.
  499. */
  500. static void
  501. pvr_page_table_l1_entry_raw_set(struct pvr_page_table_l1_entry_raw *entry,
  502. dma_addr_t child_table_dma_addr)
  503. {
  504. WRITE_ONCE(entry->val,
  505. PVR_PAGE_TABLE_FIELD_PREP(1, PD, VALID, true) |
  506. PVR_PAGE_TABLE_FIELD_PREP(1, PD, ENTRY_PENDING, false) |
  507. PVR_PAGE_TABLE_FIELD_PREP(1, PD, PAGE_SIZE, ROGUE_MMUCTRL_PAGE_SIZE_X) |
  508. /*
  509. * The use of a 4K-specific macro here is correct. It is
  510. * a future optimization to allocate sub-host-page-sized
  511. * blocks for individual tables, so the condition that any
  512. * page table address is aligned to the size of the
  513. * largest (a 4KB) table currently holds.
  514. */
  515. (child_table_dma_addr & ~ROGUE_MMUCTRL_PT_BASE_4KB_RANGE_CLRMSK));
  516. }
  517. static void
  518. pvr_page_table_l1_entry_raw_clear(struct pvr_page_table_l1_entry_raw *entry)
  519. {
  520. WRITE_ONCE(entry->val, 0);
  521. }
  522. /**
  523. * struct pvr_page_table_l0_entry_raw - A single entry in a level 0 page table.
  524. * @val: The raw value of this entry.
  525. *
  526. * This type is a structure for type-checking purposes. At compile-time, its
  527. * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PT_VALUE.
  528. *
  529. * The value stored in this structure can be decoded using the following bitmap:
  530. *
  531. * .. flat-table::
  532. * :widths: 1 5
  533. * :stub-columns: 1
  534. *
  535. * * - 63
  536. * - *(reserved)*
  537. *
  538. * * - 62
  539. * - **PM/FW Protect:** Indicates a protected region which only the
  540. * Parameter Manager (PM) or firmware processor can write to.
  541. *
  542. * * - 61..40
  543. * - **VP Page (High):** Virtual-physical page used for Parameter Manager
  544. * (PM) memory. This field is only used if the additional level of PB
  545. * virtualization is enabled. The VP Page field is needed by the PM in
  546. * order to correctly reconstitute the free lists after render
  547. * completion. This (High) field holds bits 39..18 of the value; the
  548. * Low field holds bits 17..12. Bits 11..0 are always zero because the
  549. * value is always aligned to the 4KiB page size.
  550. *
  551. * * - 39..12
  552. * - **Physical Page Address:** The way this value is interpreted depends
  553. * on the page size. Bits not specified in the table below (e.g. bits
  554. * 20..12 for page size 2MiB) should be considered reserved.
  555. *
  556. * This table shows the bits used in an L0 page table entry to represent
  557. * the Physical Page Address for a given page size (as defined in the
  558. * associated L1 page table entry).
  559. *
  560. * .. flat-table::
  561. * :widths: 1 1
  562. * :header-rows: 1
  563. * :stub-columns: 1
  564. *
  565. * * - Page size
  566. * - Physical address bits
  567. *
  568. * * - 4KiB
  569. * - 39..12
  570. *
  571. * * - 16KiB
  572. * - 39..14
  573. *
  574. * * - 64KiB
  575. * - 39..16
  576. *
  577. * * - 256KiB
  578. * - 39..18
  579. *
  580. * * - 1MiB
  581. * - 39..20
  582. *
  583. * * - 2MiB
  584. * - 39..21
  585. *
  586. * * - 11..6
  587. * - **VP Page (Low):** Continuation of VP Page (High).
  588. *
  589. * * - 5
  590. * - **Pending:** When valid bit is not set, indicates that a valid entry
  591. * is pending and the MMU should wait for the driver to map the entry.
  592. * This is used to support page demand mapping of memory.
  593. *
  594. * * - 4
  595. * - **PM Src:** Set on Parameter Manager (PM) allocated page table
  596. * entries when indicated by the PM. Note that this bit will only be set
  597. * by the PM, not by the device driver.
  598. *
  599. * * - 3
  600. * - **SLC Bypass Control:** Specifies requests to this page should bypass
  601. * the System Level Cache (SLC), if enabled in SLC configuration.
  602. *
  603. * * - 2
  604. * - **Cache Coherency:** Indicates that the page is coherent (i.e. it
  605. * does not require a cache flush between operations on the CPU and the
  606. * device).
  607. *
  608. * * - 1
  609. * - **Read Only:** If set, this bit indicates that the page is read only.
  610. * An attempted write to this page would result in a write-protection
  611. * fault.
  612. *
  613. * * - 0
  614. * - **Valid:** Indicates that the entry contains a valid page. If the
  615. * valid bit is not set, then an attempted use of the page would result
  616. * in a page fault.
  617. */
  618. struct pvr_page_table_l0_entry_raw {
  619. u64 val;
  620. } __packed;
  621. static_assert(sizeof(struct pvr_page_table_l0_entry_raw) * 8 ==
  622. ROGUE_MMUCTRL_ENTRY_SIZE_PT_VALUE);
  623. /**
  624. * struct pvr_page_flags_raw - The configurable flags from a single entry in a
  625. * level 0 page table.
  626. * @val: The raw value of these flags. Since these are a strict subset of
  627. * &struct pvr_page_table_l0_entry_raw; use that type for our member here.
  628. *
  629. * The flags stored in this type are: PM/FW Protect; SLC Bypass Control; Cache
  630. * Coherency, and Read Only (bits 62, 3, 2 and 1 respectively).
  631. *
  632. * This type should never be instantiated directly; instead use
  633. * pvr_page_flags_raw_create() to ensure only valid bits of @val are set.
  634. */
  635. struct pvr_page_flags_raw {
  636. struct pvr_page_table_l0_entry_raw val;
  637. } __packed;
  638. static_assert(sizeof(struct pvr_page_flags_raw) ==
  639. sizeof(struct pvr_page_table_l0_entry_raw));
  640. static bool
  641. pvr_page_table_l0_entry_raw_is_valid(struct pvr_page_table_l0_entry_raw entry)
  642. {
  643. return PVR_PAGE_TABLE_FIELD_GET(0, PT, VALID, entry);
  644. }
  645. /**
  646. * pvr_page_table_l0_entry_raw_set() - Write a valid entry into a raw level 0
  647. * page table.
  648. * @entry: Target raw level 0 page table entry.
  649. * @dma_addr: DMA address of the physical page to be associated with @entry.
  650. * @flags: Options to be set on @entry.
  651. *
  652. * When calling this function, @child_table_dma_addr must be a valid DMA
  653. * address and a multiple of %PVR_DEVICE_PAGE_SIZE.
  654. *
  655. * The @flags parameter is directly assigned into @entry. It is the callers
  656. * responsibility to ensure that only bits specified in
  657. * &struct pvr_page_flags_raw are set in @flags.
  658. */
  659. static void
  660. pvr_page_table_l0_entry_raw_set(struct pvr_page_table_l0_entry_raw *entry,
  661. dma_addr_t dma_addr,
  662. struct pvr_page_flags_raw flags)
  663. {
  664. WRITE_ONCE(entry->val, PVR_PAGE_TABLE_FIELD_PREP(0, PT, VALID, true) |
  665. PVR_PAGE_TABLE_FIELD_PREP(0, PT, ENTRY_PENDING, false) |
  666. (dma_addr & ~ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK) |
  667. flags.val.val);
  668. }
  669. static void
  670. pvr_page_table_l0_entry_raw_clear(struct pvr_page_table_l0_entry_raw *entry)
  671. {
  672. WRITE_ONCE(entry->val, 0);
  673. }
  674. /**
  675. * pvr_page_flags_raw_create() - Initialize the flag bits of a raw level 0 page
  676. * table entry.
  677. * @read_only: This page is read-only (see: Read Only).
  678. * @cache_coherent: This page does not require cache flushes (see: Cache
  679. * Coherency).
  680. * @slc_bypass: This page bypasses the device cache (see: SLC Bypass Control).
  681. * @pm_fw_protect: This page is only for use by the firmware or Parameter
  682. * Manager (see PM/FW Protect).
  683. *
  684. * For more details on the use of these four options, see their respective
  685. * entries in the table under &struct pvr_page_table_l0_entry_raw.
  686. *
  687. * Return:
  688. * A new &struct pvr_page_flags_raw instance which can be passed directly to
  689. * pvr_page_table_l0_entry_raw_set() or pvr_page_table_l0_insert().
  690. */
  691. static struct pvr_page_flags_raw
  692. pvr_page_flags_raw_create(bool read_only, bool cache_coherent, bool slc_bypass,
  693. bool pm_fw_protect)
  694. {
  695. struct pvr_page_flags_raw flags;
  696. flags.val.val =
  697. PVR_PAGE_TABLE_FIELD_PREP(0, PT, READ_ONLY, read_only) |
  698. PVR_PAGE_TABLE_FIELD_PREP(0, PT, CC, cache_coherent) |
  699. PVR_PAGE_TABLE_FIELD_PREP(0, PT, SLC_BYPASS_CTRL, slc_bypass) |
  700. PVR_PAGE_TABLE_FIELD_PREP(0, PT, PM_META_PROTECT, pm_fw_protect);
  701. return flags;
  702. }
  703. /**
  704. * struct pvr_page_table_l2_raw - The raw data of a level 2 page table.
  705. *
  706. * This type is a structure for type-checking purposes. At compile-time, its
  707. * size is checked against %PVR_MMU_BACKING_PAGE_SIZE.
  708. */
  709. struct pvr_page_table_l2_raw {
  710. /** @entries: The raw values of this table. */
  711. struct pvr_page_table_l2_entry_raw
  712. entries[ROGUE_MMUCTRL_ENTRIES_PC_VALUE];
  713. } __packed;
  714. static_assert(sizeof(struct pvr_page_table_l2_raw) == PVR_MMU_BACKING_PAGE_SIZE);
  715. /**
  716. * struct pvr_page_table_l1_raw - The raw data of a level 1 page table.
  717. *
  718. * This type is a structure for type-checking purposes. At compile-time, its
  719. * size is checked against %PVR_MMU_BACKING_PAGE_SIZE.
  720. */
  721. struct pvr_page_table_l1_raw {
  722. /** @entries: The raw values of this table. */
  723. struct pvr_page_table_l1_entry_raw
  724. entries[ROGUE_MMUCTRL_ENTRIES_PD_VALUE];
  725. } __packed;
  726. static_assert(sizeof(struct pvr_page_table_l1_raw) == PVR_MMU_BACKING_PAGE_SIZE);
  727. /**
  728. * struct pvr_page_table_l0_raw - The raw data of a level 0 page table.
  729. *
  730. * This type is a structure for type-checking purposes. At compile-time, its
  731. * size is checked against %PVR_MMU_BACKING_PAGE_SIZE.
  732. *
  733. * .. caution::
  734. *
  735. * The size of level 0 page tables is variable depending on the page size
  736. * specified in the associated level 1 page table entry. Since the device
  737. * page size in use is pegged to the host page size, it cannot vary at
  738. * runtime. This structure is therefore only defined to contain the required
  739. * number of entries for the current device page size. **You should never
  740. * read or write beyond the last supported entry.**
  741. */
  742. struct pvr_page_table_l0_raw {
  743. /** @entries: The raw values of this table. */
  744. struct pvr_page_table_l0_entry_raw
  745. entries[ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X];
  746. } __packed;
  747. static_assert(sizeof(struct pvr_page_table_l0_raw) <= PVR_MMU_BACKING_PAGE_SIZE);
  748. /**
  749. * DOC: Mirror page tables
  750. */
  751. /*
  752. * We pre-declare these types because they cross-depend on pointers to each
  753. * other.
  754. */
  755. struct pvr_page_table_l1;
  756. struct pvr_page_table_l0;
  757. /**
  758. * struct pvr_page_table_l2 - A wrapped level 2 page table.
  759. *
  760. * To access the raw part of this table, use pvr_page_table_l2_get_raw().
  761. * Alternatively to access a raw entry directly, use
  762. * pvr_page_table_l2_get_entry_raw().
  763. *
  764. * A level 2 page table forms the root of the page table tree structure, so
  765. * this type has no &parent or &parent_idx members.
  766. */
  767. struct pvr_page_table_l2 {
  768. /**
  769. * @entries: The children of this node in the page table tree
  770. * structure. These are also mirror tables. The indexing of this array
  771. * is identical to that of the raw equivalent
  772. * (&pvr_page_table_l1_raw.entries).
  773. */
  774. struct pvr_page_table_l1 *entries[ROGUE_MMUCTRL_ENTRIES_PC_VALUE];
  775. /**
  776. * @backing_page: A handle to the memory which holds the raw
  777. * equivalent of this table. **For internal use only.**
  778. */
  779. struct pvr_mmu_backing_page backing_page;
  780. /**
  781. * @entry_count: The current number of valid entries (that we know of)
  782. * in this table. This value is essentially a refcount - the table is
  783. * destroyed when this value is decremented to zero by
  784. * pvr_page_table_l2_remove().
  785. */
  786. u16 entry_count;
  787. };
  788. /**
  789. * pvr_page_table_l2_init() - Initialize a level 2 page table.
  790. * @table: Target level 2 page table.
  791. * @pvr_dev: Target PowerVR device
  792. *
  793. * It is expected that @table be zeroed (e.g. from kzalloc()) before calling
  794. * this function.
  795. *
  796. * Return:
  797. * * 0 on success, or
  798. * * Any error encountered while intializing &table->backing_page using
  799. * pvr_mmu_backing_page_init().
  800. */
  801. static int
  802. pvr_page_table_l2_init(struct pvr_page_table_l2 *table,
  803. struct pvr_device *pvr_dev)
  804. {
  805. return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev);
  806. }
  807. /**
  808. * pvr_page_table_l2_fini() - Teardown a level 2 page table.
  809. * @table: Target level 2 page table.
  810. *
  811. * It is an error to attempt to use @table after calling this function.
  812. */
  813. static void
  814. pvr_page_table_l2_fini(struct pvr_page_table_l2 *table)
  815. {
  816. pvr_mmu_backing_page_fini(&table->backing_page);
  817. }
  818. /**
  819. * pvr_page_table_l2_sync() - Flush a level 2 page table from the CPU to the
  820. * device.
  821. * @table: Target level 2 page table.
  822. *
  823. * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the
  824. * warning there applies here too: **Only call pvr_page_table_l2_sync() once
  825. * you're sure you have no more changes to make to** @table **in the immediate
  826. * future.**
  827. *
  828. * If child level 1 page tables of @table also need to be flushed, this should
  829. * be done first using pvr_page_table_l1_sync() *before* calling this function.
  830. */
  831. static void
  832. pvr_page_table_l2_sync(struct pvr_page_table_l2 *table)
  833. {
  834. pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_2_FLAGS);
  835. }
  836. /**
  837. * pvr_page_table_l2_get_raw() - Access the raw equivalent of a mirror level 2
  838. * page table.
  839. * @table: Target level 2 page table.
  840. *
  841. * Essentially returns the CPU address of the raw equivalent of @table, cast to
  842. * a &struct pvr_page_table_l2_raw pointer.
  843. *
  844. * You probably want to call pvr_page_table_l2_get_entry_raw() instead.
  845. *
  846. * Return:
  847. * The raw equivalent of @table.
  848. */
  849. static struct pvr_page_table_l2_raw *
  850. pvr_page_table_l2_get_raw(struct pvr_page_table_l2 *table)
  851. {
  852. return table->backing_page.host_ptr;
  853. }
  854. /**
  855. * pvr_page_table_l2_get_entry_raw() - Access an entry from the raw equivalent
  856. * of a mirror level 2 page table.
  857. * @table: Target level 2 page table.
  858. * @idx: Index of the entry to access.
  859. *
  860. * Technically this function returns a pointer to a slot in a raw level 2 page
  861. * table, since the returned "entry" is not guaranteed to be valid. The caller
  862. * must verify the validity of the entry at the returned address (perhaps using
  863. * pvr_page_table_l2_entry_raw_is_valid()) before reading or overwriting it.
  864. *
  865. * The value of @idx is not checked here; it is the callers responsibility to
  866. * ensure @idx refers to a valid index within @table before dereferencing the
  867. * returned pointer.
  868. *
  869. * Return:
  870. * A pointer to the requested raw level 2 page table entry.
  871. */
  872. static struct pvr_page_table_l2_entry_raw *
  873. pvr_page_table_l2_get_entry_raw(struct pvr_page_table_l2 *table, u16 idx)
  874. {
  875. return &pvr_page_table_l2_get_raw(table)->entries[idx];
  876. }
  877. /**
  878. * pvr_page_table_l2_entry_is_valid() - Check if a level 2 page table entry is
  879. * marked as valid.
  880. * @table: Target level 2 page table.
  881. * @idx: Index of the entry to check.
  882. *
  883. * The value of @idx is not checked here; it is the callers responsibility to
  884. * ensure @idx refers to a valid index within @table before calling this
  885. * function.
  886. */
  887. static bool
  888. pvr_page_table_l2_entry_is_valid(struct pvr_page_table_l2 *table, u16 idx)
  889. {
  890. struct pvr_page_table_l2_entry_raw entry_raw =
  891. *pvr_page_table_l2_get_entry_raw(table, idx);
  892. return pvr_page_table_l2_entry_raw_is_valid(entry_raw);
  893. }
  894. /**
  895. * struct pvr_page_table_l1 - A wrapped level 1 page table.
  896. *
  897. * To access the raw part of this table, use pvr_page_table_l1_get_raw().
  898. * Alternatively to access a raw entry directly, use
  899. * pvr_page_table_l1_get_entry_raw().
  900. */
  901. struct pvr_page_table_l1 {
  902. /**
  903. * @entries: The children of this node in the page table tree
  904. * structure. These are also mirror tables. The indexing of this array
  905. * is identical to that of the raw equivalent
  906. * (&pvr_page_table_l0_raw.entries).
  907. */
  908. struct pvr_page_table_l0 *entries[ROGUE_MMUCTRL_ENTRIES_PD_VALUE];
  909. /**
  910. * @backing_page: A handle to the memory which holds the raw
  911. * equivalent of this table. **For internal use only.**
  912. */
  913. struct pvr_mmu_backing_page backing_page;
  914. union {
  915. /**
  916. * @parent: The parent of this node in the page table tree structure.
  917. *
  918. * This is also a mirror table.
  919. *
  920. * Only valid when the L1 page table is active. When the L1 page table
  921. * has been removed and queued for destruction, the next_free field
  922. * should be used instead.
  923. */
  924. struct pvr_page_table_l2 *parent;
  925. /**
  926. * @next_free: Pointer to the next L1 page table to take/free.
  927. *
  928. * Used to form a linked list of L1 page tables. This is used
  929. * when preallocating tables and when the page table has been
  930. * removed and queued for destruction.
  931. */
  932. struct pvr_page_table_l1 *next_free;
  933. };
  934. /**
  935. * @parent_idx: The index of the entry in the parent table (see
  936. * @parent) which corresponds to this table.
  937. */
  938. u16 parent_idx;
  939. /**
  940. * @entry_count: The current number of valid entries (that we know of)
  941. * in this table. This value is essentially a refcount - the table is
  942. * destroyed when this value is decremented to zero by
  943. * pvr_page_table_l1_remove().
  944. */
  945. u16 entry_count;
  946. };
  947. /**
  948. * pvr_page_table_l1_init() - Initialize a level 1 page table.
  949. * @table: Target level 1 page table.
  950. * @pvr_dev: Target PowerVR device
  951. *
  952. * When this function returns successfully, @table is still not considered
  953. * valid. It must be inserted into the page table tree structure with
  954. * pvr_page_table_l2_insert() before it is ready for use.
  955. *
  956. * It is expected that @table be zeroed (e.g. from kzalloc()) before calling
  957. * this function.
  958. *
  959. * Return:
  960. * * 0 on success, or
  961. * * Any error encountered while intializing &table->backing_page using
  962. * pvr_mmu_backing_page_init().
  963. */
  964. static int
  965. pvr_page_table_l1_init(struct pvr_page_table_l1 *table,
  966. struct pvr_device *pvr_dev)
  967. {
  968. table->parent_idx = PVR_IDX_INVALID;
  969. return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev);
  970. }
  971. /**
  972. * pvr_page_table_l1_free() - Teardown a level 1 page table.
  973. * @table: Target level 1 page table.
  974. *
  975. * It is an error to attempt to use @table after calling this function, even
  976. * indirectly. This includes calling pvr_page_table_l2_remove(), which must
  977. * be called *before* pvr_page_table_l1_free().
  978. */
  979. static void
  980. pvr_page_table_l1_free(struct pvr_page_table_l1 *table)
  981. {
  982. pvr_mmu_backing_page_fini(&table->backing_page);
  983. kfree(table);
  984. }
  985. /**
  986. * pvr_page_table_l1_sync() - Flush a level 1 page table from the CPU to the
  987. * device.
  988. * @table: Target level 1 page table.
  989. *
  990. * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the
  991. * warning there applies here too: **Only call pvr_page_table_l1_sync() once
  992. * you're sure you have no more changes to make to** @table **in the immediate
  993. * future.**
  994. *
  995. * If child level 0 page tables of @table also need to be flushed, this should
  996. * be done first using pvr_page_table_l0_sync() *before* calling this function.
  997. */
  998. static void
  999. pvr_page_table_l1_sync(struct pvr_page_table_l1 *table)
  1000. {
  1001. pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_1_FLAGS);
  1002. }
  1003. /**
  1004. * pvr_page_table_l1_get_raw() - Access the raw equivalent of a mirror level 1
  1005. * page table.
  1006. * @table: Target level 1 page table.
  1007. *
  1008. * Essentially returns the CPU address of the raw equivalent of @table, cast to
  1009. * a &struct pvr_page_table_l1_raw pointer.
  1010. *
  1011. * You probably want to call pvr_page_table_l1_get_entry_raw() instead.
  1012. *
  1013. * Return:
  1014. * The raw equivalent of @table.
  1015. */
  1016. static struct pvr_page_table_l1_raw *
  1017. pvr_page_table_l1_get_raw(struct pvr_page_table_l1 *table)
  1018. {
  1019. return table->backing_page.host_ptr;
  1020. }
  1021. /**
  1022. * pvr_page_table_l1_get_entry_raw() - Access an entry from the raw equivalent
  1023. * of a mirror level 1 page table.
  1024. * @table: Target level 1 page table.
  1025. * @idx: Index of the entry to access.
  1026. *
  1027. * Technically this function returns a pointer to a slot in a raw level 1 page
  1028. * table, since the returned "entry" is not guaranteed to be valid. The caller
  1029. * must verify the validity of the entry at the returned address (perhaps using
  1030. * pvr_page_table_l1_entry_raw_is_valid()) before reading or overwriting it.
  1031. *
  1032. * The value of @idx is not checked here; it is the callers responsibility to
  1033. * ensure @idx refers to a valid index within @table before dereferencing the
  1034. * returned pointer.
  1035. *
  1036. * Return:
  1037. * A pointer to the requested raw level 1 page table entry.
  1038. */
  1039. static struct pvr_page_table_l1_entry_raw *
  1040. pvr_page_table_l1_get_entry_raw(struct pvr_page_table_l1 *table, u16 idx)
  1041. {
  1042. return &pvr_page_table_l1_get_raw(table)->entries[idx];
  1043. }
  1044. /**
  1045. * pvr_page_table_l1_entry_is_valid() - Check if a level 1 page table entry is
  1046. * marked as valid.
  1047. * @table: Target level 1 page table.
  1048. * @idx: Index of the entry to check.
  1049. *
  1050. * The value of @idx is not checked here; it is the callers responsibility to
  1051. * ensure @idx refers to a valid index within @table before calling this
  1052. * function.
  1053. */
  1054. static bool
  1055. pvr_page_table_l1_entry_is_valid(struct pvr_page_table_l1 *table, u16 idx)
  1056. {
  1057. struct pvr_page_table_l1_entry_raw entry_raw =
  1058. *pvr_page_table_l1_get_entry_raw(table, idx);
  1059. return pvr_page_table_l1_entry_raw_is_valid(entry_raw);
  1060. }
  1061. /**
  1062. * struct pvr_page_table_l0 - A wrapped level 0 page table.
  1063. *
  1064. * To access the raw part of this table, use pvr_page_table_l0_get_raw().
  1065. * Alternatively to access a raw entry directly, use
  1066. * pvr_page_table_l0_get_entry_raw().
  1067. *
  1068. * There is no mirror representation of an individual page, so this type has no
  1069. * &entries member.
  1070. */
  1071. struct pvr_page_table_l0 {
  1072. /**
  1073. * @backing_page: A handle to the memory which holds the raw
  1074. * equivalent of this table. **For internal use only.**
  1075. */
  1076. struct pvr_mmu_backing_page backing_page;
  1077. union {
  1078. /**
  1079. * @parent: The parent of this node in the page table tree structure.
  1080. *
  1081. * This is also a mirror table.
  1082. *
  1083. * Only valid when the L0 page table is active. When the L0 page table
  1084. * has been removed and queued for destruction, the next_free field
  1085. * should be used instead.
  1086. */
  1087. struct pvr_page_table_l1 *parent;
  1088. /**
  1089. * @next_free: Pointer to the next L0 page table to take/free.
  1090. *
  1091. * Used to form a linked list of L0 page tables. This is used
  1092. * when preallocating tables and when the page table has been
  1093. * removed and queued for destruction.
  1094. */
  1095. struct pvr_page_table_l0 *next_free;
  1096. };
  1097. /**
  1098. * @parent_idx: The index of the entry in the parent table (see
  1099. * @parent) which corresponds to this table.
  1100. */
  1101. u16 parent_idx;
  1102. /**
  1103. * @entry_count: The current number of valid entries (that we know of)
  1104. * in this table. This value is essentially a refcount - the table is
  1105. * destroyed when this value is decremented to zero by
  1106. * pvr_page_table_l0_remove().
  1107. */
  1108. u16 entry_count;
  1109. };
  1110. /**
  1111. * pvr_page_table_l0_init() - Initialize a level 0 page table.
  1112. * @table: Target level 0 page table.
  1113. * @pvr_dev: Target PowerVR device
  1114. *
  1115. * When this function returns successfully, @table is still not considered
  1116. * valid. It must be inserted into the page table tree structure with
  1117. * pvr_page_table_l1_insert() before it is ready for use.
  1118. *
  1119. * It is expected that @table be zeroed (e.g. from kzalloc()) before calling
  1120. * this function.
  1121. *
  1122. * Return:
  1123. * * 0 on success, or
  1124. * * Any error encountered while intializing &table->backing_page using
  1125. * pvr_mmu_backing_page_init().
  1126. */
  1127. static int
  1128. pvr_page_table_l0_init(struct pvr_page_table_l0 *table,
  1129. struct pvr_device *pvr_dev)
  1130. {
  1131. table->parent_idx = PVR_IDX_INVALID;
  1132. return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev);
  1133. }
  1134. /**
  1135. * pvr_page_table_l0_free() - Teardown a level 0 page table.
  1136. * @table: Target level 0 page table.
  1137. *
  1138. * It is an error to attempt to use @table after calling this function, even
  1139. * indirectly. This includes calling pvr_page_table_l1_remove(), which must
  1140. * be called *before* pvr_page_table_l0_free().
  1141. */
  1142. static void
  1143. pvr_page_table_l0_free(struct pvr_page_table_l0 *table)
  1144. {
  1145. pvr_mmu_backing_page_fini(&table->backing_page);
  1146. kfree(table);
  1147. }
  1148. /**
  1149. * pvr_page_table_l0_sync() - Flush a level 0 page table from the CPU to the
  1150. * device.
  1151. * @table: Target level 0 page table.
  1152. *
  1153. * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the
  1154. * warning there applies here too: **Only call pvr_page_table_l0_sync() once
  1155. * you're sure you have no more changes to make to** @table **in the immediate
  1156. * future.**
  1157. *
  1158. * If child pages of @table also need to be flushed, this should be done first
  1159. * using a DMA sync function (e.g. dma_sync_sg_for_device()) *before* calling
  1160. * this function.
  1161. */
  1162. static void
  1163. pvr_page_table_l0_sync(struct pvr_page_table_l0 *table)
  1164. {
  1165. pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_0_FLAGS);
  1166. }
  1167. /**
  1168. * pvr_page_table_l0_get_raw() - Access the raw equivalent of a mirror level 0
  1169. * page table.
  1170. * @table: Target level 0 page table.
  1171. *
  1172. * Essentially returns the CPU address of the raw equivalent of @table, cast to
  1173. * a &struct pvr_page_table_l0_raw pointer.
  1174. *
  1175. * You probably want to call pvr_page_table_l0_get_entry_raw() instead.
  1176. *
  1177. * Return:
  1178. * The raw equivalent of @table.
  1179. */
  1180. static struct pvr_page_table_l0_raw *
  1181. pvr_page_table_l0_get_raw(struct pvr_page_table_l0 *table)
  1182. {
  1183. return table->backing_page.host_ptr;
  1184. }
  1185. /**
  1186. * pvr_page_table_l0_get_entry_raw() - Access an entry from the raw equivalent
  1187. * of a mirror level 0 page table.
  1188. * @table: Target level 0 page table.
  1189. * @idx: Index of the entry to access.
  1190. *
  1191. * Technically this function returns a pointer to a slot in a raw level 0 page
  1192. * table, since the returned "entry" is not guaranteed to be valid. The caller
  1193. * must verify the validity of the entry at the returned address (perhaps using
  1194. * pvr_page_table_l0_entry_raw_is_valid()) before reading or overwriting it.
  1195. *
  1196. * The value of @idx is not checked here; it is the callers responsibility to
  1197. * ensure @idx refers to a valid index within @table before dereferencing the
  1198. * returned pointer. This is espcially important for level 0 page tables, which
  1199. * can have a variable number of entries.
  1200. *
  1201. * Return:
  1202. * A pointer to the requested raw level 0 page table entry.
  1203. */
  1204. static struct pvr_page_table_l0_entry_raw *
  1205. pvr_page_table_l0_get_entry_raw(struct pvr_page_table_l0 *table, u16 idx)
  1206. {
  1207. return &pvr_page_table_l0_get_raw(table)->entries[idx];
  1208. }
  1209. /**
  1210. * pvr_page_table_l0_entry_is_valid() - Check if a level 0 page table entry is
  1211. * marked as valid.
  1212. * @table: Target level 0 page table.
  1213. * @idx: Index of the entry to check.
  1214. *
  1215. * The value of @idx is not checked here; it is the callers responsibility to
  1216. * ensure @idx refers to a valid index within @table before calling this
  1217. * function.
  1218. */
  1219. static bool
  1220. pvr_page_table_l0_entry_is_valid(struct pvr_page_table_l0 *table, u16 idx)
  1221. {
  1222. struct pvr_page_table_l0_entry_raw entry_raw =
  1223. *pvr_page_table_l0_get_entry_raw(table, idx);
  1224. return pvr_page_table_l0_entry_raw_is_valid(entry_raw);
  1225. }
  1226. /**
  1227. * struct pvr_mmu_context - context holding data for operations at page
  1228. * catalogue level, intended for use with a VM context.
  1229. */
  1230. struct pvr_mmu_context {
  1231. /** @pvr_dev: The PVR device associated with the owning VM context. */
  1232. struct pvr_device *pvr_dev;
  1233. /** @page_table_l2: The MMU table root. */
  1234. struct pvr_page_table_l2 page_table_l2;
  1235. };
  1236. /**
  1237. * struct pvr_page_table_ptr - A reference to a single physical page as indexed
  1238. * by the page table structure.
  1239. *
  1240. * Intended for embedding in a &struct pvr_mmu_op_context.
  1241. */
  1242. struct pvr_page_table_ptr {
  1243. /**
  1244. * @l1_table: A cached handle to the level 1 page table the
  1245. * context is currently traversing.
  1246. */
  1247. struct pvr_page_table_l1 *l1_table;
  1248. /**
  1249. * @l0_table: A cached handle to the level 0 page table the
  1250. * context is currently traversing.
  1251. */
  1252. struct pvr_page_table_l0 *l0_table;
  1253. /**
  1254. * @l2_idx: Index into the level 2 page table the context is
  1255. * currently referencing.
  1256. */
  1257. u16 l2_idx;
  1258. /**
  1259. * @l1_idx: Index into the level 1 page table the context is
  1260. * currently referencing.
  1261. */
  1262. u16 l1_idx;
  1263. /**
  1264. * @l0_idx: Index into the level 0 page table the context is
  1265. * currently referencing.
  1266. */
  1267. u16 l0_idx;
  1268. };
  1269. /**
  1270. * struct pvr_mmu_op_context - context holding data for individual
  1271. * device-virtual mapping operations. Intended for use with a VM bind operation.
  1272. */
  1273. struct pvr_mmu_op_context {
  1274. /** @mmu_ctx: The MMU context associated with the owning VM context. */
  1275. struct pvr_mmu_context *mmu_ctx;
  1276. /** @map: Data specifically for map operations. */
  1277. struct {
  1278. /**
  1279. * @sgt: Scatter gather table containing pages pinned for use by
  1280. * this context - these are currently pinned when initialising
  1281. * the VM bind operation.
  1282. */
  1283. struct sg_table *sgt;
  1284. /** @sgt_offset: Start address of the device-virtual mapping. */
  1285. u64 sgt_offset;
  1286. /**
  1287. * @l1_prealloc_tables: Preallocated l1 page table objects
  1288. * use by this context when creating a page mapping. Linked list
  1289. * fully created during initialisation.
  1290. */
  1291. struct pvr_page_table_l1 *l1_prealloc_tables;
  1292. /**
  1293. * @l0_prealloc_tables: Preallocated l0 page table objects
  1294. * use by this context when creating a page mapping. Linked list
  1295. * fully created during initialisation.
  1296. */
  1297. struct pvr_page_table_l0 *l0_prealloc_tables;
  1298. } map;
  1299. /** @unmap: Data specifically for unmap operations. */
  1300. struct {
  1301. /**
  1302. * @l1_free_tables: Collects page table objects freed by unmap
  1303. * ops. Linked list empty at creation.
  1304. */
  1305. struct pvr_page_table_l1 *l1_free_tables;
  1306. /**
  1307. * @l0_free_tables: Collects page table objects freed by unmap
  1308. * ops. Linked list empty at creation.
  1309. */
  1310. struct pvr_page_table_l0 *l0_free_tables;
  1311. } unmap;
  1312. /**
  1313. * @curr_page: A reference to a single physical page as indexed by the
  1314. * page table structure.
  1315. */
  1316. struct pvr_page_table_ptr curr_page;
  1317. /**
  1318. * @sync_level_required: The maximum level of the page table tree
  1319. * structure which has (possibly) been modified since it was last
  1320. * flushed to the device.
  1321. *
  1322. * This field should only be set with pvr_mmu_op_context_require_sync()
  1323. * or indirectly by pvr_mmu_op_context_sync_partial().
  1324. */
  1325. enum pvr_mmu_sync_level sync_level_required;
  1326. };
  1327. /**
  1328. * pvr_page_table_l2_insert() - Insert an entry referring to a level 1 page
  1329. * table into a level 2 page table.
  1330. * @op_ctx: Target MMU op context pointing at the entry to insert the L1 page
  1331. * table into.
  1332. * @child_table: Target level 1 page table to be referenced by the new entry.
  1333. *
  1334. * It is the caller's responsibility to ensure @op_ctx.curr_page points to a
  1335. * valid L2 entry.
  1336. *
  1337. * It is the caller's responsibility to execute any memory barries to ensure
  1338. * that the creation of @child_table is ordered before the L2 entry is inserted.
  1339. */
  1340. static void
  1341. pvr_page_table_l2_insert(struct pvr_mmu_op_context *op_ctx,
  1342. struct pvr_page_table_l1 *child_table)
  1343. {
  1344. struct pvr_page_table_l2 *l2_table =
  1345. &op_ctx->mmu_ctx->page_table_l2;
  1346. struct pvr_page_table_l2_entry_raw *entry_raw =
  1347. pvr_page_table_l2_get_entry_raw(l2_table,
  1348. op_ctx->curr_page.l2_idx);
  1349. pvr_page_table_l2_entry_raw_set(entry_raw,
  1350. child_table->backing_page.dma_addr);
  1351. child_table->parent = l2_table;
  1352. child_table->parent_idx = op_ctx->curr_page.l2_idx;
  1353. l2_table->entries[op_ctx->curr_page.l2_idx] = child_table;
  1354. ++l2_table->entry_count;
  1355. op_ctx->curr_page.l1_table = child_table;
  1356. }
  1357. /**
  1358. * pvr_page_table_l2_remove() - Remove a level 1 page table from a level 2 page
  1359. * table.
  1360. * @op_ctx: Target MMU op context pointing at the L2 entry to remove.
  1361. *
  1362. * It is the caller's responsibility to ensure @op_ctx.curr_page points to a
  1363. * valid L2 entry.
  1364. */
  1365. static void
  1366. pvr_page_table_l2_remove(struct pvr_mmu_op_context *op_ctx)
  1367. {
  1368. struct pvr_page_table_l2 *l2_table =
  1369. &op_ctx->mmu_ctx->page_table_l2;
  1370. struct pvr_page_table_l2_entry_raw *entry_raw =
  1371. pvr_page_table_l2_get_entry_raw(l2_table,
  1372. op_ctx->curr_page.l1_table->parent_idx);
  1373. WARN_ON(op_ctx->curr_page.l1_table->parent != l2_table);
  1374. pvr_page_table_l2_entry_raw_clear(entry_raw);
  1375. l2_table->entries[op_ctx->curr_page.l1_table->parent_idx] = NULL;
  1376. op_ctx->curr_page.l1_table->parent_idx = PVR_IDX_INVALID;
  1377. op_ctx->curr_page.l1_table->next_free = op_ctx->unmap.l1_free_tables;
  1378. op_ctx->unmap.l1_free_tables = op_ctx->curr_page.l1_table;
  1379. op_ctx->curr_page.l1_table = NULL;
  1380. --l2_table->entry_count;
  1381. }
  1382. /**
  1383. * pvr_page_table_l1_insert() - Insert an entry referring to a level 0 page
  1384. * table into a level 1 page table.
  1385. * @op_ctx: Target MMU op context pointing at the entry to insert the L0 page
  1386. * table into.
  1387. * @child_table: L0 page table to insert.
  1388. *
  1389. * It is the caller's responsibility to ensure @op_ctx.curr_page points to a
  1390. * valid L1 entry.
  1391. *
  1392. * It is the caller's responsibility to execute any memory barries to ensure
  1393. * that the creation of @child_table is ordered before the L1 entry is inserted.
  1394. */
  1395. static void
  1396. pvr_page_table_l1_insert(struct pvr_mmu_op_context *op_ctx,
  1397. struct pvr_page_table_l0 *child_table)
  1398. {
  1399. struct pvr_page_table_l1_entry_raw *entry_raw =
  1400. pvr_page_table_l1_get_entry_raw(op_ctx->curr_page.l1_table,
  1401. op_ctx->curr_page.l1_idx);
  1402. pvr_page_table_l1_entry_raw_set(entry_raw,
  1403. child_table->backing_page.dma_addr);
  1404. child_table->parent = op_ctx->curr_page.l1_table;
  1405. child_table->parent_idx = op_ctx->curr_page.l1_idx;
  1406. op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l1_idx] = child_table;
  1407. ++op_ctx->curr_page.l1_table->entry_count;
  1408. op_ctx->curr_page.l0_table = child_table;
  1409. }
  1410. /**
  1411. * pvr_page_table_l1_remove() - Remove a level 0 page table from a level 1 page
  1412. * table.
  1413. * @op_ctx: Target MMU op context pointing at the L1 entry to remove.
  1414. *
  1415. * If this function results in the L1 table becoming empty, it will be removed
  1416. * from its parent level 2 page table and destroyed.
  1417. *
  1418. * It is the caller's responsibility to ensure @op_ctx.curr_page points to a
  1419. * valid L1 entry.
  1420. */
  1421. static void
  1422. pvr_page_table_l1_remove(struct pvr_mmu_op_context *op_ctx)
  1423. {
  1424. struct pvr_page_table_l1_entry_raw *entry_raw =
  1425. pvr_page_table_l1_get_entry_raw(op_ctx->curr_page.l0_table->parent,
  1426. op_ctx->curr_page.l0_table->parent_idx);
  1427. WARN_ON(op_ctx->curr_page.l0_table->parent !=
  1428. op_ctx->curr_page.l1_table);
  1429. pvr_page_table_l1_entry_raw_clear(entry_raw);
  1430. op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l0_table->parent_idx] = NULL;
  1431. op_ctx->curr_page.l0_table->parent_idx = PVR_IDX_INVALID;
  1432. op_ctx->curr_page.l0_table->next_free = op_ctx->unmap.l0_free_tables;
  1433. op_ctx->unmap.l0_free_tables = op_ctx->curr_page.l0_table;
  1434. op_ctx->curr_page.l0_table = NULL;
  1435. if (--op_ctx->curr_page.l1_table->entry_count == 0) {
  1436. /* Clear the parent L2 page table entry. */
  1437. if (op_ctx->curr_page.l1_table->parent_idx != PVR_IDX_INVALID)
  1438. pvr_page_table_l2_remove(op_ctx);
  1439. }
  1440. }
  1441. /**
  1442. * pvr_page_table_l0_insert() - Insert an entry referring to a physical page
  1443. * into a level 0 page table.
  1444. * @op_ctx: Target MMU op context pointing at the L0 entry to insert.
  1445. * @dma_addr: Target DMA address to be referenced by the new entry.
  1446. * @flags: Page options to be stored in the new entry.
  1447. *
  1448. * It is the caller's responsibility to ensure @op_ctx.curr_page points to a
  1449. * valid L0 entry.
  1450. */
  1451. static void
  1452. pvr_page_table_l0_insert(struct pvr_mmu_op_context *op_ctx,
  1453. dma_addr_t dma_addr, struct pvr_page_flags_raw flags)
  1454. {
  1455. struct pvr_page_table_l0_entry_raw *entry_raw =
  1456. pvr_page_table_l0_get_entry_raw(op_ctx->curr_page.l0_table,
  1457. op_ctx->curr_page.l0_idx);
  1458. pvr_page_table_l0_entry_raw_set(entry_raw, dma_addr, flags);
  1459. /*
  1460. * There is no entry to set here - we don't keep a mirror of
  1461. * individual pages.
  1462. */
  1463. ++op_ctx->curr_page.l0_table->entry_count;
  1464. }
  1465. /**
  1466. * pvr_page_table_l0_remove() - Remove a physical page from a level 0 page
  1467. * table.
  1468. * @op_ctx: Target MMU op context pointing at the L0 entry to remove.
  1469. *
  1470. * If this function results in the L0 table becoming empty, it will be removed
  1471. * from its parent L1 page table and destroyed.
  1472. *
  1473. * It is the caller's responsibility to ensure @op_ctx.curr_page points to a
  1474. * valid L0 entry.
  1475. */
  1476. static void
  1477. pvr_page_table_l0_remove(struct pvr_mmu_op_context *op_ctx)
  1478. {
  1479. struct pvr_page_table_l0_entry_raw *entry_raw =
  1480. pvr_page_table_l0_get_entry_raw(op_ctx->curr_page.l0_table,
  1481. op_ctx->curr_page.l0_idx);
  1482. pvr_page_table_l0_entry_raw_clear(entry_raw);
  1483. /*
  1484. * There is no entry to clear here - we don't keep a mirror of
  1485. * individual pages.
  1486. */
  1487. if (--op_ctx->curr_page.l0_table->entry_count == 0) {
  1488. /* Clear the parent L1 page table entry. */
  1489. if (op_ctx->curr_page.l0_table->parent_idx != PVR_IDX_INVALID)
  1490. pvr_page_table_l1_remove(op_ctx);
  1491. }
  1492. }
  1493. /**
  1494. * DOC: Page table index utilities
  1495. */
  1496. /**
  1497. * pvr_page_table_l2_idx() - Calculate the level 2 page table index for a
  1498. * device-virtual address.
  1499. * @device_addr: Target device-virtual address.
  1500. *
  1501. * This function does not perform any bounds checking - it is the caller's
  1502. * responsibility to ensure that @device_addr is valid before interpreting
  1503. * the result.
  1504. *
  1505. * Return:
  1506. * The index into a level 2 page table corresponding to @device_addr.
  1507. */
  1508. static u16
  1509. pvr_page_table_l2_idx(u64 device_addr)
  1510. {
  1511. return (device_addr & ~ROGUE_MMUCTRL_VADDR_PC_INDEX_CLRMSK) >>
  1512. ROGUE_MMUCTRL_VADDR_PC_INDEX_SHIFT;
  1513. }
  1514. /**
  1515. * pvr_page_table_l1_idx() - Calculate the level 1 page table index for a
  1516. * device-virtual address.
  1517. * @device_addr: Target device-virtual address.
  1518. *
  1519. * This function does not perform any bounds checking - it is the caller's
  1520. * responsibility to ensure that @device_addr is valid before interpreting
  1521. * the result.
  1522. *
  1523. * Return:
  1524. * The index into a level 1 page table corresponding to @device_addr.
  1525. */
  1526. static u16
  1527. pvr_page_table_l1_idx(u64 device_addr)
  1528. {
  1529. return (device_addr & ~ROGUE_MMUCTRL_VADDR_PD_INDEX_CLRMSK) >>
  1530. ROGUE_MMUCTRL_VADDR_PD_INDEX_SHIFT;
  1531. }
  1532. /**
  1533. * pvr_page_table_l0_idx() - Calculate the level 0 page table index for a
  1534. * device-virtual address.
  1535. * @device_addr: Target device-virtual address.
  1536. *
  1537. * This function does not perform any bounds checking - it is the caller's
  1538. * responsibility to ensure that @device_addr is valid before interpreting
  1539. * the result.
  1540. *
  1541. * Return:
  1542. * The index into a level 0 page table corresponding to @device_addr.
  1543. */
  1544. static u16
  1545. pvr_page_table_l0_idx(u64 device_addr)
  1546. {
  1547. return (device_addr & ~ROGUE_MMUCTRL_VADDR_PT_INDEX_CLRMSK) >>
  1548. ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT;
  1549. }
  1550. /**
  1551. * DOC: High-level page table operations
  1552. */
  1553. /**
  1554. * pvr_page_table_l1_get_or_insert() - Retrieves (optionally inserting if
  1555. * necessary) a level 1 page table from the specified level 2 page table entry.
  1556. * @op_ctx: Target MMU op context.
  1557. * @should_insert: [IN] Specifies whether new page tables should be inserted
  1558. * when empty page table entries are encountered during traversal.
  1559. *
  1560. * Return:
  1561. * * 0 on success, or
  1562. *
  1563. * If @should_insert is %false:
  1564. * * -%ENXIO if a level 1 page table would have been inserted.
  1565. *
  1566. * If @should_insert is %true:
  1567. * * Any error encountered while inserting the level 1 page table.
  1568. */
  1569. static int
  1570. pvr_page_table_l1_get_or_insert(struct pvr_mmu_op_context *op_ctx,
  1571. bool should_insert)
  1572. {
  1573. struct pvr_page_table_l2 *l2_table =
  1574. &op_ctx->mmu_ctx->page_table_l2;
  1575. struct pvr_page_table_l1 *table;
  1576. if (pvr_page_table_l2_entry_is_valid(l2_table,
  1577. op_ctx->curr_page.l2_idx)) {
  1578. op_ctx->curr_page.l1_table =
  1579. l2_table->entries[op_ctx->curr_page.l2_idx];
  1580. return 0;
  1581. }
  1582. if (!should_insert)
  1583. return -ENXIO;
  1584. /* Take a prealloced table. */
  1585. table = op_ctx->map.l1_prealloc_tables;
  1586. if (!table)
  1587. return -ENOMEM;
  1588. /* Pop */
  1589. op_ctx->map.l1_prealloc_tables = table->next_free;
  1590. table->next_free = NULL;
  1591. /* Ensure new table is fully written out before adding to L2 page table. */
  1592. wmb();
  1593. pvr_page_table_l2_insert(op_ctx, table);
  1594. return 0;
  1595. }
  1596. /**
  1597. * pvr_page_table_l0_get_or_insert() - Retrieves (optionally inserting if
  1598. * necessary) a level 0 page table from the specified level 1 page table entry.
  1599. * @op_ctx: Target MMU op context.
  1600. * @should_insert: [IN] Specifies whether new page tables should be inserted
  1601. * when empty page table entries are encountered during traversal.
  1602. *
  1603. * Return:
  1604. * * 0 on success,
  1605. *
  1606. * If @should_insert is %false:
  1607. * * -%ENXIO if a level 0 page table would have been inserted.
  1608. *
  1609. * If @should_insert is %true:
  1610. * * Any error encountered while inserting the level 0 page table.
  1611. */
  1612. static int
  1613. pvr_page_table_l0_get_or_insert(struct pvr_mmu_op_context *op_ctx,
  1614. bool should_insert)
  1615. {
  1616. struct pvr_page_table_l0 *table;
  1617. if (pvr_page_table_l1_entry_is_valid(op_ctx->curr_page.l1_table,
  1618. op_ctx->curr_page.l1_idx)) {
  1619. op_ctx->curr_page.l0_table =
  1620. op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l1_idx];
  1621. return 0;
  1622. }
  1623. if (!should_insert)
  1624. return -ENXIO;
  1625. /* Take a prealloced table. */
  1626. table = op_ctx->map.l0_prealloc_tables;
  1627. if (!table)
  1628. return -ENOMEM;
  1629. /* Pop */
  1630. op_ctx->map.l0_prealloc_tables = table->next_free;
  1631. table->next_free = NULL;
  1632. /* Ensure new table is fully written out before adding to L1 page table. */
  1633. wmb();
  1634. pvr_page_table_l1_insert(op_ctx, table);
  1635. return 0;
  1636. }
  1637. /**
  1638. * pvr_mmu_context_create() - Create an MMU context.
  1639. * @pvr_dev: PVR device associated with owning VM context.
  1640. *
  1641. * Returns:
  1642. * * Newly created MMU context object on success, or
  1643. * * -%ENOMEM if no memory is available,
  1644. * * Any error code returned by pvr_page_table_l2_init().
  1645. */
  1646. struct pvr_mmu_context *pvr_mmu_context_create(struct pvr_device *pvr_dev)
  1647. {
  1648. struct pvr_mmu_context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1649. int err;
  1650. if (!ctx)
  1651. return ERR_PTR(-ENOMEM);
  1652. err = pvr_page_table_l2_init(&ctx->page_table_l2, pvr_dev);
  1653. if (err)
  1654. return ERR_PTR(err);
  1655. ctx->pvr_dev = pvr_dev;
  1656. return ctx;
  1657. }
  1658. /**
  1659. * pvr_mmu_context_destroy() - Destroy an MMU context.
  1660. * @ctx: Target MMU context.
  1661. */
  1662. void pvr_mmu_context_destroy(struct pvr_mmu_context *ctx)
  1663. {
  1664. pvr_page_table_l2_fini(&ctx->page_table_l2);
  1665. kfree(ctx);
  1666. }
  1667. /**
  1668. * pvr_mmu_get_root_table_dma_addr() - Get the DMA address of the root of the
  1669. * page table structure behind a VM context.
  1670. * @ctx: Target MMU context.
  1671. */
  1672. dma_addr_t pvr_mmu_get_root_table_dma_addr(struct pvr_mmu_context *ctx)
  1673. {
  1674. return ctx->page_table_l2.backing_page.dma_addr;
  1675. }
  1676. /**
  1677. * pvr_page_table_l1_alloc() - Allocate a l1 page_table object.
  1678. * @ctx: MMU context of owning VM context.
  1679. *
  1680. * Returns:
  1681. * * Newly created page table object on success, or
  1682. * * -%ENOMEM if no memory is available,
  1683. * * Any error code returned by pvr_page_table_l1_init().
  1684. */
  1685. static struct pvr_page_table_l1 *
  1686. pvr_page_table_l1_alloc(struct pvr_mmu_context *ctx)
  1687. {
  1688. int err;
  1689. struct pvr_page_table_l1 *table =
  1690. kzalloc(sizeof(*table), GFP_KERNEL);
  1691. if (!table)
  1692. return ERR_PTR(-ENOMEM);
  1693. err = pvr_page_table_l1_init(table, ctx->pvr_dev);
  1694. if (err) {
  1695. kfree(table);
  1696. return ERR_PTR(err);
  1697. }
  1698. return table;
  1699. }
  1700. /**
  1701. * pvr_page_table_l0_alloc() - Allocate a l0 page_table object.
  1702. * @ctx: MMU context of owning VM context.
  1703. *
  1704. * Returns:
  1705. * * Newly created page table object on success, or
  1706. * * -%ENOMEM if no memory is available,
  1707. * * Any error code returned by pvr_page_table_l0_init().
  1708. */
  1709. static struct pvr_page_table_l0 *
  1710. pvr_page_table_l0_alloc(struct pvr_mmu_context *ctx)
  1711. {
  1712. int err;
  1713. struct pvr_page_table_l0 *table =
  1714. kzalloc(sizeof(*table), GFP_KERNEL);
  1715. if (!table)
  1716. return ERR_PTR(-ENOMEM);
  1717. err = pvr_page_table_l0_init(table, ctx->pvr_dev);
  1718. if (err) {
  1719. kfree(table);
  1720. return ERR_PTR(err);
  1721. }
  1722. return table;
  1723. }
  1724. /**
  1725. * pvr_mmu_op_context_require_sync() - Mark an MMU op context as requiring a
  1726. * sync operation for the referenced page tables up to a specified level.
  1727. * @op_ctx: Target MMU op context.
  1728. * @level: Maximum page table level for which a sync is required.
  1729. */
  1730. static void
  1731. pvr_mmu_op_context_require_sync(struct pvr_mmu_op_context *op_ctx,
  1732. enum pvr_mmu_sync_level level)
  1733. {
  1734. if (op_ctx->sync_level_required < level)
  1735. op_ctx->sync_level_required = level;
  1736. }
  1737. /**
  1738. * pvr_mmu_op_context_sync_manual() - Trigger a sync of some or all of the
  1739. * page tables referenced by a MMU op context.
  1740. * @op_ctx: Target MMU op context.
  1741. * @level: Maximum page table level to sync.
  1742. *
  1743. * Do not call this function directly. Instead use
  1744. * pvr_mmu_op_context_sync_partial() which is checked against the current
  1745. * value of &op_ctx->sync_level_required as set by
  1746. * pvr_mmu_op_context_require_sync().
  1747. */
  1748. static void
  1749. pvr_mmu_op_context_sync_manual(struct pvr_mmu_op_context *op_ctx,
  1750. enum pvr_mmu_sync_level level)
  1751. {
  1752. /*
  1753. * We sync the page table levels in ascending order (starting from the
  1754. * leaf node) to ensure consistency.
  1755. */
  1756. WARN_ON(level < PVR_MMU_SYNC_LEVEL_NONE);
  1757. if (level <= PVR_MMU_SYNC_LEVEL_NONE)
  1758. return;
  1759. if (op_ctx->curr_page.l0_table)
  1760. pvr_page_table_l0_sync(op_ctx->curr_page.l0_table);
  1761. if (level < PVR_MMU_SYNC_LEVEL_1)
  1762. return;
  1763. if (op_ctx->curr_page.l1_table)
  1764. pvr_page_table_l1_sync(op_ctx->curr_page.l1_table);
  1765. if (level < PVR_MMU_SYNC_LEVEL_2)
  1766. return;
  1767. pvr_page_table_l2_sync(&op_ctx->mmu_ctx->page_table_l2);
  1768. }
  1769. /**
  1770. * pvr_mmu_op_context_sync_partial() - Trigger a sync of some or all of the
  1771. * page tables referenced by a MMU op context.
  1772. * @op_ctx: Target MMU op context.
  1773. * @level: Requested page table level to sync up to (inclusive).
  1774. *
  1775. * If @level is greater than the maximum level recorded by @op_ctx as requiring
  1776. * a sync operation, only the previously recorded maximum will be used.
  1777. *
  1778. * Additionally, if @level is greater than or equal to the maximum level
  1779. * recorded by @op_ctx as requiring a sync operation, that maximum level will be
  1780. * reset as a full sync will be performed. This is equivalent to calling
  1781. * pvr_mmu_op_context_sync().
  1782. */
  1783. static void
  1784. pvr_mmu_op_context_sync_partial(struct pvr_mmu_op_context *op_ctx,
  1785. enum pvr_mmu_sync_level level)
  1786. {
  1787. /*
  1788. * If the requested sync level is greater than or equal to the
  1789. * currently required sync level, we do two things:
  1790. * * Don't waste time syncing levels we haven't previously marked as
  1791. * requiring a sync, and
  1792. * * Reset the required sync level since we are about to sync
  1793. * everything that was previously marked as requiring a sync.
  1794. */
  1795. if (level >= op_ctx->sync_level_required) {
  1796. level = op_ctx->sync_level_required;
  1797. op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE;
  1798. }
  1799. pvr_mmu_op_context_sync_manual(op_ctx, level);
  1800. }
  1801. /**
  1802. * pvr_mmu_op_context_sync() - Trigger a sync of every page table referenced by
  1803. * a MMU op context.
  1804. * @op_ctx: Target MMU op context.
  1805. *
  1806. * The maximum level marked internally as requiring a sync will be reset so
  1807. * that subsequent calls to this function will be no-ops unless @op_ctx is
  1808. * otherwise updated.
  1809. */
  1810. static void
  1811. pvr_mmu_op_context_sync(struct pvr_mmu_op_context *op_ctx)
  1812. {
  1813. pvr_mmu_op_context_sync_manual(op_ctx, op_ctx->sync_level_required);
  1814. op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE;
  1815. }
  1816. /**
  1817. * pvr_mmu_op_context_load_tables() - Load pointers to tables in each level of
  1818. * the page table tree structure needed to reference the physical page
  1819. * referenced by a MMU op context.
  1820. * @op_ctx: Target MMU op context.
  1821. * @should_create: Specifies whether new page tables should be created when
  1822. * empty page table entries are encountered during traversal.
  1823. * @load_level_required: Maximum page table level to load.
  1824. *
  1825. * If @should_create is %true, this function may modify the stored required
  1826. * sync level of @op_ctx as new page tables are created and inserted into their
  1827. * respective parents.
  1828. *
  1829. * Since there is only one root page table, it is technically incorrect to call
  1830. * this function with a value of @load_level_required greater than or equal to
  1831. * the root level number. However, this is not explicitly disallowed here.
  1832. *
  1833. * Return:
  1834. * * 0 on success,
  1835. * * Any error returned by pvr_page_table_l1_get_or_create() if
  1836. * @load_level_required >= 1 except -%ENXIO, or
  1837. * * Any error returned by pvr_page_table_l0_get_or_create() if
  1838. * @load_level_required >= 0 except -%ENXIO.
  1839. */
  1840. static int
  1841. pvr_mmu_op_context_load_tables(struct pvr_mmu_op_context *op_ctx,
  1842. bool should_create,
  1843. enum pvr_mmu_sync_level load_level_required)
  1844. {
  1845. const struct pvr_page_table_l1 *l1_head_before =
  1846. op_ctx->map.l1_prealloc_tables;
  1847. const struct pvr_page_table_l0 *l0_head_before =
  1848. op_ctx->map.l0_prealloc_tables;
  1849. int err;
  1850. /* Clear tables we're about to fetch in case of error states. */
  1851. if (load_level_required >= PVR_MMU_SYNC_LEVEL_1)
  1852. op_ctx->curr_page.l1_table = NULL;
  1853. if (load_level_required >= PVR_MMU_SYNC_LEVEL_0)
  1854. op_ctx->curr_page.l0_table = NULL;
  1855. /* Get or create L1 page table. */
  1856. if (load_level_required >= PVR_MMU_SYNC_LEVEL_1) {
  1857. err = pvr_page_table_l1_get_or_insert(op_ctx, should_create);
  1858. if (err) {
  1859. /*
  1860. * If @should_create is %false and no L1 page table was
  1861. * found, return early but without an error. Since
  1862. * pvr_page_table_l1_get_or_create() can only return
  1863. * -%ENXIO if @should_create is %false, there is no
  1864. * need to check it here.
  1865. */
  1866. if (err == -ENXIO)
  1867. err = 0;
  1868. return err;
  1869. }
  1870. }
  1871. /* Get or create L0 page table. */
  1872. if (load_level_required >= PVR_MMU_SYNC_LEVEL_0) {
  1873. err = pvr_page_table_l0_get_or_insert(op_ctx, should_create);
  1874. if (err) {
  1875. /*
  1876. * If @should_create is %false and no L0 page table was
  1877. * found, return early but without an error. Since
  1878. * pvr_page_table_l0_get_or_insert() can only return
  1879. * -%ENXIO if @should_create is %false, there is no
  1880. * need to check it here.
  1881. */
  1882. if (err == -ENXIO)
  1883. err = 0;
  1884. /*
  1885. * At this point, an L1 page table could have been
  1886. * inserted but is now empty due to the failed attempt
  1887. * at inserting an L0 page table. In this instance, we
  1888. * must remove the empty L1 page table ourselves as
  1889. * pvr_page_table_l1_remove() is never called as part
  1890. * of the error path in
  1891. * pvr_page_table_l0_get_or_insert().
  1892. */
  1893. if (l1_head_before != op_ctx->map.l1_prealloc_tables) {
  1894. pvr_page_table_l2_remove(op_ctx);
  1895. pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_2);
  1896. }
  1897. return err;
  1898. }
  1899. }
  1900. /*
  1901. * A sync is only needed if table objects were inserted. This can be
  1902. * inferred by checking if the pointer at the head of the linked list
  1903. * has changed.
  1904. */
  1905. if (l1_head_before != op_ctx->map.l1_prealloc_tables)
  1906. pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_2);
  1907. else if (l0_head_before != op_ctx->map.l0_prealloc_tables)
  1908. pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_1);
  1909. return 0;
  1910. }
  1911. /**
  1912. * pvr_mmu_op_context_set_curr_page() - Reassign the current page of an MMU op
  1913. * context, syncing any page tables previously assigned to it which are no
  1914. * longer relevant.
  1915. * @op_ctx: Target MMU op context.
  1916. * @device_addr: New pointer target.
  1917. * @should_create: Specify whether new page tables should be created when
  1918. * empty page table entries are encountered during traversal.
  1919. *
  1920. * This function performs a full sync on the pointer, regardless of which
  1921. * levels are modified.
  1922. *
  1923. * Return:
  1924. * * 0 on success, or
  1925. * * Any error returned by pvr_mmu_op_context_load_tables().
  1926. */
  1927. static int
  1928. pvr_mmu_op_context_set_curr_page(struct pvr_mmu_op_context *op_ctx,
  1929. u64 device_addr, bool should_create)
  1930. {
  1931. pvr_mmu_op_context_sync(op_ctx);
  1932. op_ctx->curr_page.l2_idx = pvr_page_table_l2_idx(device_addr);
  1933. op_ctx->curr_page.l1_idx = pvr_page_table_l1_idx(device_addr);
  1934. op_ctx->curr_page.l0_idx = pvr_page_table_l0_idx(device_addr);
  1935. op_ctx->curr_page.l1_table = NULL;
  1936. op_ctx->curr_page.l0_table = NULL;
  1937. return pvr_mmu_op_context_load_tables(op_ctx, should_create,
  1938. PVR_MMU_SYNC_LEVEL_1);
  1939. }
  1940. /**
  1941. * pvr_mmu_op_context_next_page() - Advance the current page of an MMU op
  1942. * context.
  1943. * @op_ctx: Target MMU op context.
  1944. * @should_create: Specify whether new page tables should be created when
  1945. * empty page table entries are encountered during traversal.
  1946. *
  1947. * If @should_create is %false, it is the caller's responsibility to verify that
  1948. * the state of the table references in @op_ctx is valid on return. If -%ENXIO
  1949. * is returned, at least one of the table references is invalid. It should be
  1950. * noted that @op_ctx as a whole will be left in a valid state if -%ENXIO is
  1951. * returned, unlike other error codes. The caller should check which references
  1952. * are invalid by comparing them to %NULL. Only &@ptr->l2_table is guaranteed
  1953. * to be valid, since it represents the root of the page table tree structure.
  1954. *
  1955. * Return:
  1956. * * 0 on success,
  1957. * * -%EPERM if the operation would wrap at the top of the page table
  1958. * hierarchy,
  1959. * * -%ENXIO if @should_create is %false and a page table of any level would
  1960. * have otherwise been created, or
  1961. * * Any error returned while attempting to create missing page tables if
  1962. * @should_create is %true.
  1963. */
  1964. static int
  1965. pvr_mmu_op_context_next_page(struct pvr_mmu_op_context *op_ctx,
  1966. bool should_create)
  1967. {
  1968. s8 load_level_required = PVR_MMU_SYNC_LEVEL_NONE;
  1969. if (++op_ctx->curr_page.l0_idx != ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X)
  1970. goto load_tables;
  1971. op_ctx->curr_page.l0_idx = 0;
  1972. load_level_required = PVR_MMU_SYNC_LEVEL_0;
  1973. if (++op_ctx->curr_page.l1_idx != ROGUE_MMUCTRL_ENTRIES_PD_VALUE)
  1974. goto load_tables;
  1975. op_ctx->curr_page.l1_idx = 0;
  1976. load_level_required = PVR_MMU_SYNC_LEVEL_1;
  1977. if (++op_ctx->curr_page.l2_idx != ROGUE_MMUCTRL_ENTRIES_PC_VALUE)
  1978. goto load_tables;
  1979. /*
  1980. * If the pattern continued, we would set &op_ctx->curr_page.l2_idx to
  1981. * zero here. However, that would wrap the top layer of the page table
  1982. * hierarchy which is not a valid operation. Instead, we warn and return
  1983. * an error.
  1984. */
  1985. WARN(true,
  1986. "%s(%p) attempted to loop the top of the page table hierarchy",
  1987. __func__, op_ctx);
  1988. return -EPERM;
  1989. /* If indices have wrapped, we need to load new tables. */
  1990. load_tables:
  1991. /* First, flush tables which will be unloaded. */
  1992. pvr_mmu_op_context_sync_partial(op_ctx, load_level_required);
  1993. /* Then load tables from the required level down. */
  1994. return pvr_mmu_op_context_load_tables(op_ctx, should_create,
  1995. load_level_required);
  1996. }
  1997. /**
  1998. * DOC: Single page operations
  1999. */
  2000. /**
  2001. * pvr_page_create() - Create a device-virtual memory page and insert it into
  2002. * a level 0 page table.
  2003. * @op_ctx: Target MMU op context pointing at the device-virtual address of the
  2004. * target page.
  2005. * @dma_addr: DMA address of the physical page backing the created page.
  2006. * @flags: Page options saved on the level 0 page table entry for reading by
  2007. * the device.
  2008. *
  2009. * Return:
  2010. * * 0 on success, or
  2011. * * -%EEXIST if the requested page already exists.
  2012. */
  2013. static int
  2014. pvr_page_create(struct pvr_mmu_op_context *op_ctx, dma_addr_t dma_addr,
  2015. struct pvr_page_flags_raw flags)
  2016. {
  2017. /* Do not create a new page if one already exists. */
  2018. if (pvr_page_table_l0_entry_is_valid(op_ctx->curr_page.l0_table,
  2019. op_ctx->curr_page.l0_idx)) {
  2020. return -EEXIST;
  2021. }
  2022. pvr_page_table_l0_insert(op_ctx, dma_addr, flags);
  2023. pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0);
  2024. return 0;
  2025. }
  2026. /**
  2027. * pvr_page_destroy() - Destroy a device page after removing it from its
  2028. * parent level 0 page table.
  2029. * @op_ctx: Target MMU op context.
  2030. */
  2031. static void
  2032. pvr_page_destroy(struct pvr_mmu_op_context *op_ctx)
  2033. {
  2034. /* Do nothing if the page does not exist. */
  2035. if (!pvr_page_table_l0_entry_is_valid(op_ctx->curr_page.l0_table,
  2036. op_ctx->curr_page.l0_idx)) {
  2037. return;
  2038. }
  2039. /* Clear the parent L0 page table entry. */
  2040. pvr_page_table_l0_remove(op_ctx);
  2041. pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0);
  2042. }
  2043. /**
  2044. * pvr_mmu_op_context_destroy() - Destroy an MMU op context.
  2045. * @op_ctx: Target MMU op context.
  2046. */
  2047. void pvr_mmu_op_context_destroy(struct pvr_mmu_op_context *op_ctx)
  2048. {
  2049. const bool flush_caches =
  2050. op_ctx->sync_level_required != PVR_MMU_SYNC_LEVEL_NONE;
  2051. pvr_mmu_op_context_sync(op_ctx);
  2052. /* Unmaps should be flushed immediately. Map flushes can be deferred. */
  2053. if (flush_caches && !op_ctx->map.sgt)
  2054. pvr_mmu_flush_exec(op_ctx->mmu_ctx->pvr_dev, true);
  2055. while (op_ctx->map.l0_prealloc_tables) {
  2056. struct pvr_page_table_l0 *tmp = op_ctx->map.l0_prealloc_tables;
  2057. op_ctx->map.l0_prealloc_tables =
  2058. op_ctx->map.l0_prealloc_tables->next_free;
  2059. pvr_page_table_l0_free(tmp);
  2060. }
  2061. while (op_ctx->map.l1_prealloc_tables) {
  2062. struct pvr_page_table_l1 *tmp = op_ctx->map.l1_prealloc_tables;
  2063. op_ctx->map.l1_prealloc_tables =
  2064. op_ctx->map.l1_prealloc_tables->next_free;
  2065. pvr_page_table_l1_free(tmp);
  2066. }
  2067. while (op_ctx->unmap.l0_free_tables) {
  2068. struct pvr_page_table_l0 *tmp = op_ctx->unmap.l0_free_tables;
  2069. op_ctx->unmap.l0_free_tables =
  2070. op_ctx->unmap.l0_free_tables->next_free;
  2071. pvr_page_table_l0_free(tmp);
  2072. }
  2073. while (op_ctx->unmap.l1_free_tables) {
  2074. struct pvr_page_table_l1 *tmp = op_ctx->unmap.l1_free_tables;
  2075. op_ctx->unmap.l1_free_tables =
  2076. op_ctx->unmap.l1_free_tables->next_free;
  2077. pvr_page_table_l1_free(tmp);
  2078. }
  2079. kfree(op_ctx);
  2080. }
  2081. /**
  2082. * pvr_mmu_op_context_create() - Create an MMU op context.
  2083. * @ctx: MMU context associated with owning VM context.
  2084. * @sgt: Scatter gather table containing pages pinned for use by this context.
  2085. * @sgt_offset: Start offset of the requested device-virtual memory mapping.
  2086. * @size: Size in bytes of the requested device-virtual memory mapping. For an
  2087. * unmapping, this should be zero so that no page tables are allocated.
  2088. *
  2089. * Returns:
  2090. * * Newly created MMU op context object on success, or
  2091. * * -%ENOMEM if no memory is available,
  2092. * * Any error code returned by pvr_page_table_l2_init().
  2093. */
  2094. struct pvr_mmu_op_context *
  2095. pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, struct sg_table *sgt,
  2096. u64 sgt_offset, u64 size)
  2097. {
  2098. int err;
  2099. struct pvr_mmu_op_context *op_ctx =
  2100. kzalloc(sizeof(*op_ctx), GFP_KERNEL);
  2101. if (!op_ctx)
  2102. return ERR_PTR(-ENOMEM);
  2103. op_ctx->mmu_ctx = ctx;
  2104. op_ctx->map.sgt = sgt;
  2105. op_ctx->map.sgt_offset = sgt_offset;
  2106. op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE;
  2107. if (size) {
  2108. /*
  2109. * The number of page table objects we need to prealloc is
  2110. * indicated by the mapping size, start offset and the sizes
  2111. * of the areas mapped per PT or PD. The range calculation is
  2112. * identical to that for the index into a table for a device
  2113. * address, so we reuse those functions here.
  2114. */
  2115. const u32 l1_start_idx = pvr_page_table_l2_idx(sgt_offset);
  2116. const u32 l1_end_idx = pvr_page_table_l2_idx(sgt_offset + size);
  2117. const u32 l1_count = l1_end_idx - l1_start_idx + 1;
  2118. const u32 l0_start_idx = pvr_page_table_l1_idx(sgt_offset);
  2119. const u32 l0_end_idx = pvr_page_table_l1_idx(sgt_offset + size);
  2120. const u32 l0_count = l0_end_idx - l0_start_idx + 1;
  2121. /*
  2122. * Alloc and push page table entries until we have enough of
  2123. * each type, ending with linked lists of l0 and l1 entries in
  2124. * reverse order.
  2125. */
  2126. for (int i = 0; i < l1_count; i++) {
  2127. struct pvr_page_table_l1 *l1_tmp =
  2128. pvr_page_table_l1_alloc(ctx);
  2129. err = PTR_ERR_OR_ZERO(l1_tmp);
  2130. if (err)
  2131. goto err_cleanup;
  2132. l1_tmp->next_free = op_ctx->map.l1_prealloc_tables;
  2133. op_ctx->map.l1_prealloc_tables = l1_tmp;
  2134. }
  2135. for (int i = 0; i < l0_count; i++) {
  2136. struct pvr_page_table_l0 *l0_tmp =
  2137. pvr_page_table_l0_alloc(ctx);
  2138. err = PTR_ERR_OR_ZERO(l0_tmp);
  2139. if (err)
  2140. goto err_cleanup;
  2141. l0_tmp->next_free = op_ctx->map.l0_prealloc_tables;
  2142. op_ctx->map.l0_prealloc_tables = l0_tmp;
  2143. }
  2144. }
  2145. return op_ctx;
  2146. err_cleanup:
  2147. pvr_mmu_op_context_destroy(op_ctx);
  2148. return ERR_PTR(err);
  2149. }
  2150. /**
  2151. * pvr_mmu_op_context_unmap_curr_page() - Unmap pages from a memory context
  2152. * starting from the current page of an MMU op context.
  2153. * @op_ctx: Target MMU op context pointing at the first page to unmap.
  2154. * @nr_pages: Number of pages to unmap.
  2155. *
  2156. * Return:
  2157. * * 0 on success, or
  2158. * * Any error encountered while advancing @op_ctx.curr_page with
  2159. * pvr_mmu_op_context_next_page() (except -%ENXIO).
  2160. */
  2161. static int
  2162. pvr_mmu_op_context_unmap_curr_page(struct pvr_mmu_op_context *op_ctx,
  2163. u64 nr_pages)
  2164. {
  2165. int err;
  2166. if (nr_pages == 0)
  2167. return 0;
  2168. /*
  2169. * Destroy first page outside loop, as it doesn't require a page
  2170. * advance beforehand. If the L0 page table reference in
  2171. * @op_ctx.curr_page is %NULL, there cannot be a mapped page at
  2172. * @op_ctx.curr_page (so skip ahead).
  2173. */
  2174. if (op_ctx->curr_page.l0_table)
  2175. pvr_page_destroy(op_ctx);
  2176. for (u64 page = 1; page < nr_pages; ++page) {
  2177. err = pvr_mmu_op_context_next_page(op_ctx, false);
  2178. /*
  2179. * If the page table tree structure at @op_ctx.curr_page is
  2180. * incomplete, skip ahead. We don't care about unmapping pages
  2181. * that cannot exist.
  2182. *
  2183. * FIXME: This could be made more efficient by jumping ahead
  2184. * using pvr_mmu_op_context_set_curr_page().
  2185. */
  2186. if (err == -ENXIO)
  2187. continue;
  2188. else if (err)
  2189. return err;
  2190. pvr_page_destroy(op_ctx);
  2191. }
  2192. return 0;
  2193. }
  2194. /**
  2195. * pvr_mmu_unmap() - Unmap pages from a memory context.
  2196. * @op_ctx: Target MMU op context.
  2197. * @device_addr: First device-virtual address to unmap.
  2198. * @size: Size in bytes to unmap.
  2199. *
  2200. * The total amount of device-virtual memory unmapped is
  2201. * @nr_pages * %PVR_DEVICE_PAGE_SIZE.
  2202. *
  2203. * Returns:
  2204. * * 0 on success, or
  2205. * * Any error code returned by pvr_page_table_ptr_init(), or
  2206. * * Any error code returned by pvr_page_table_ptr_unmap().
  2207. */
  2208. int pvr_mmu_unmap(struct pvr_mmu_op_context *op_ctx, u64 device_addr, u64 size)
  2209. {
  2210. int err = pvr_mmu_op_context_set_curr_page(op_ctx, device_addr, false);
  2211. if (err)
  2212. return err;
  2213. return pvr_mmu_op_context_unmap_curr_page(op_ctx,
  2214. size >> PVR_DEVICE_PAGE_SHIFT);
  2215. }
  2216. /**
  2217. * pvr_mmu_map_sgl() - Map part of a scatter-gather table entry to
  2218. * device-virtual memory.
  2219. * @op_ctx: Target MMU op context pointing to the first page that should be
  2220. * mapped.
  2221. * @sgl: Target scatter-gather table entry.
  2222. * @offset: Offset into @sgl to map from. Must result in a starting address
  2223. * from @sgl which is CPU page-aligned.
  2224. * @size: Size of the memory to be mapped in bytes. Must be a non-zero multiple
  2225. * of the device page size.
  2226. * @page_flags: Page options to be applied to every device-virtual memory page
  2227. * in the created mapping.
  2228. *
  2229. * Return:
  2230. * * 0 on success,
  2231. * * -%EINVAL if the range specified by @offset and @size is not completely
  2232. * within @sgl, or
  2233. * * Any error encountered while creating a page with pvr_page_create(), or
  2234. * * Any error encountered while advancing @op_ctx.curr_page with
  2235. * pvr_mmu_op_context_next_page().
  2236. */
  2237. static int
  2238. pvr_mmu_map_sgl(struct pvr_mmu_op_context *op_ctx, struct scatterlist *sgl,
  2239. u64 offset, u64 size, struct pvr_page_flags_raw page_flags)
  2240. {
  2241. const unsigned int pages = size >> PVR_DEVICE_PAGE_SHIFT;
  2242. dma_addr_t dma_addr = sg_dma_address(sgl) + offset;
  2243. const unsigned int dma_len = sg_dma_len(sgl);
  2244. struct pvr_page_table_ptr ptr_copy;
  2245. unsigned int page;
  2246. int err;
  2247. if (size > dma_len || offset > dma_len - size)
  2248. return -EINVAL;
  2249. /*
  2250. * Before progressing, save a copy of the start pointer so we can use
  2251. * it again if we enter an error state and have to destroy pages.
  2252. */
  2253. memcpy(&ptr_copy, &op_ctx->curr_page, sizeof(ptr_copy));
  2254. /*
  2255. * Create first page outside loop, as it doesn't require a page advance
  2256. * beforehand.
  2257. */
  2258. err = pvr_page_create(op_ctx, dma_addr, page_flags);
  2259. if (err)
  2260. return err;
  2261. for (page = 1; page < pages; ++page) {
  2262. err = pvr_mmu_op_context_next_page(op_ctx, true);
  2263. if (err)
  2264. goto err_destroy_pages;
  2265. dma_addr += PVR_DEVICE_PAGE_SIZE;
  2266. err = pvr_page_create(op_ctx, dma_addr, page_flags);
  2267. if (err)
  2268. goto err_destroy_pages;
  2269. }
  2270. return 0;
  2271. err_destroy_pages:
  2272. memcpy(&op_ctx->curr_page, &ptr_copy, sizeof(op_ctx->curr_page));
  2273. err = pvr_mmu_op_context_unmap_curr_page(op_ctx, page);
  2274. return err;
  2275. }
  2276. /**
  2277. * pvr_mmu_map() - Map an object's virtual memory to physical memory.
  2278. * @op_ctx: Target MMU op context.
  2279. * @size: Size of memory to be mapped in bytes. Must be a non-zero multiple
  2280. * of the device page size.
  2281. * @flags: Flags from pvr_gem_object associated with the mapping.
  2282. * @device_addr: Virtual device address to map to. Must be device page-aligned.
  2283. *
  2284. * Returns:
  2285. * * 0 on success, or
  2286. * * Any error code returned by pvr_page_table_ptr_init(), or
  2287. * * Any error code returned by pvr_mmu_map_sgl(), or
  2288. * * Any error code returned by pvr_page_table_ptr_next_page().
  2289. */
  2290. int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags,
  2291. u64 device_addr)
  2292. {
  2293. struct pvr_page_table_ptr ptr_copy;
  2294. struct pvr_page_flags_raw flags_raw;
  2295. struct scatterlist *sgl;
  2296. u64 mapped_size = 0;
  2297. unsigned int count;
  2298. int err;
  2299. if (!size)
  2300. return 0;
  2301. if ((op_ctx->map.sgt_offset | size) & ~PVR_DEVICE_PAGE_MASK)
  2302. return -EINVAL;
  2303. err = pvr_mmu_op_context_set_curr_page(op_ctx, device_addr, true);
  2304. if (err)
  2305. return -EINVAL;
  2306. memcpy(&ptr_copy, &op_ctx->curr_page, sizeof(ptr_copy));
  2307. flags_raw = pvr_page_flags_raw_create(false, false,
  2308. flags & DRM_PVR_BO_BYPASS_DEVICE_CACHE,
  2309. flags & DRM_PVR_BO_PM_FW_PROTECT);
  2310. /* Map scatter gather table */
  2311. for_each_sgtable_dma_sg(op_ctx->map.sgt, sgl, count) {
  2312. const size_t sgl_len = sg_dma_len(sgl);
  2313. u64 sgl_offset, map_sgl_len;
  2314. if (sgl_len <= op_ctx->map.sgt_offset) {
  2315. op_ctx->map.sgt_offset -= sgl_len;
  2316. continue;
  2317. }
  2318. sgl_offset = op_ctx->map.sgt_offset;
  2319. map_sgl_len = min_t(u64, sgl_len - sgl_offset, size - mapped_size);
  2320. err = pvr_mmu_map_sgl(op_ctx, sgl, sgl_offset, map_sgl_len,
  2321. flags_raw);
  2322. if (err)
  2323. break;
  2324. /*
  2325. * Flag the L0 page table as requiring a flush when the MMU op
  2326. * context is destroyed.
  2327. */
  2328. pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0);
  2329. op_ctx->map.sgt_offset = 0;
  2330. mapped_size += map_sgl_len;
  2331. if (mapped_size >= size)
  2332. break;
  2333. err = pvr_mmu_op_context_next_page(op_ctx, true);
  2334. if (err)
  2335. break;
  2336. }
  2337. if (err && mapped_size) {
  2338. memcpy(&op_ctx->curr_page, &ptr_copy, sizeof(op_ctx->curr_page));
  2339. pvr_mmu_op_context_unmap_curr_page(op_ctx,
  2340. mapped_size >> PVR_DEVICE_PAGE_SHIFT);
  2341. }
  2342. return err;
  2343. }