amdgpu_vm.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/dma-fence-array.h>
  29. #include <drm/drmP.h>
  30. #include <drm/amdgpu_drm.h>
  31. #include "amdgpu.h"
  32. #include "amdgpu_trace.h"
  33. /*
  34. * GPUVM
  35. * GPUVM is similar to the legacy gart on older asics, however
  36. * rather than there being a single global gart table
  37. * for the entire GPU, there are multiple VM page tables active
  38. * at any given time. The VM page tables can contain a mix
  39. * vram pages and system memory pages and system memory pages
  40. * can be mapped as snooped (cached system pages) or unsnooped
  41. * (uncached system pages).
  42. * Each VM has an ID associated with it and there is a page table
  43. * associated with each VMID. When execting a command buffer,
  44. * the kernel tells the the ring what VMID to use for that command
  45. * buffer. VMIDs are allocated dynamically as commands are submitted.
  46. * The userspace drivers maintain their own address space and the kernel
  47. * sets up their pages tables accordingly when they submit their
  48. * command buffers and a VMID is assigned.
  49. * Cayman/Trinity support up to 8 active VMs at any given time;
  50. * SI supports 16.
  51. */
  52. /* Local structure. Encapsulate some VM table update parameters to reduce
  53. * the number of function parameters
  54. */
  55. struct amdgpu_pte_update_params {
  56. /* amdgpu device we do this update for */
  57. struct amdgpu_device *adev;
  58. /* optional amdgpu_vm we do this update for */
  59. struct amdgpu_vm *vm;
  60. /* address where to copy page table entries from */
  61. uint64_t src;
  62. /* indirect buffer to fill with commands */
  63. struct amdgpu_ib *ib;
  64. /* Function which actually does the update */
  65. void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
  66. uint64_t addr, unsigned count, uint32_t incr,
  67. uint64_t flags);
  68. /* indicate update pt or its shadow */
  69. bool shadow;
  70. };
  71. /* Helper to disable partial resident texture feature from a fence callback */
  72. struct amdgpu_prt_cb {
  73. struct amdgpu_device *adev;
  74. struct dma_fence_cb cb;
  75. };
  76. /**
  77. * amdgpu_vm_num_entries - return the number of entries in a PD/PT
  78. *
  79. * @adev: amdgpu_device pointer
  80. *
  81. * Calculate the number of entries in a page directory or page table.
  82. */
  83. static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
  84. unsigned level)
  85. {
  86. if (level == 0)
  87. /* For the root directory */
  88. return adev->vm_manager.max_pfn >>
  89. (amdgpu_vm_block_size * adev->vm_manager.num_level);
  90. else if (level == adev->vm_manager.num_level)
  91. /* For the page tables on the leaves */
  92. return AMDGPU_VM_PTE_COUNT;
  93. else
  94. /* Everything in between */
  95. return 1 << amdgpu_vm_block_size;
  96. }
  97. /**
  98. * amdgpu_vm_bo_size - returns the size of the BOs in bytes
  99. *
  100. * @adev: amdgpu_device pointer
  101. *
  102. * Calculate the size of the BO for a page directory or page table in bytes.
  103. */
  104. static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
  105. {
  106. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
  107. }
  108. /**
  109. * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
  110. *
  111. * @vm: vm providing the BOs
  112. * @validated: head of validation list
  113. * @entry: entry to add
  114. *
  115. * Add the page directory to the list of BOs to
  116. * validate for command submission.
  117. */
  118. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  119. struct list_head *validated,
  120. struct amdgpu_bo_list_entry *entry)
  121. {
  122. entry->robj = vm->root.bo;
  123. entry->priority = 0;
  124. entry->tv.bo = &entry->robj->tbo;
  125. entry->tv.shared = true;
  126. entry->user_pages = NULL;
  127. list_add(&entry->tv.head, validated);
  128. }
  129. /**
  130. * amdgpu_vm_validate_layer - validate a single page table level
  131. *
  132. * @parent: parent page table level
  133. * @validate: callback to do the validation
  134. * @param: parameter for the validation callback
  135. *
  136. * Validate the page table BOs on command submission if neccessary.
  137. */
  138. static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
  139. int (*validate)(void *, struct amdgpu_bo *),
  140. void *param)
  141. {
  142. unsigned i;
  143. int r;
  144. if (!parent->entries)
  145. return 0;
  146. for (i = 0; i <= parent->last_entry_used; ++i) {
  147. struct amdgpu_vm_pt *entry = &parent->entries[i];
  148. if (!entry->bo)
  149. continue;
  150. r = validate(param, entry->bo);
  151. if (r)
  152. return r;
  153. /*
  154. * Recurse into the sub directory. This is harmless because we
  155. * have only a maximum of 5 layers.
  156. */
  157. r = amdgpu_vm_validate_level(entry, validate, param);
  158. if (r)
  159. return r;
  160. }
  161. return r;
  162. }
  163. /**
  164. * amdgpu_vm_validate_pt_bos - validate the page table BOs
  165. *
  166. * @adev: amdgpu device pointer
  167. * @vm: vm providing the BOs
  168. * @validate: callback to do the validation
  169. * @param: parameter for the validation callback
  170. *
  171. * Validate the page table BOs on command submission if neccessary.
  172. */
  173. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  174. int (*validate)(void *p, struct amdgpu_bo *bo),
  175. void *param)
  176. {
  177. uint64_t num_evictions;
  178. /* We only need to validate the page tables
  179. * if they aren't already valid.
  180. */
  181. num_evictions = atomic64_read(&adev->num_evictions);
  182. if (num_evictions == vm->last_eviction_counter)
  183. return 0;
  184. return amdgpu_vm_validate_level(&vm->root, validate, param);
  185. }
  186. /**
  187. * amdgpu_vm_move_level_in_lru - move one level of PT BOs to the LRU tail
  188. *
  189. * @adev: amdgpu device instance
  190. * @vm: vm providing the BOs
  191. *
  192. * Move the PT BOs to the tail of the LRU.
  193. */
  194. static void amdgpu_vm_move_level_in_lru(struct amdgpu_vm_pt *parent)
  195. {
  196. unsigned i;
  197. if (!parent->entries)
  198. return;
  199. for (i = 0; i <= parent->last_entry_used; ++i) {
  200. struct amdgpu_vm_pt *entry = &parent->entries[i];
  201. if (!entry->bo)
  202. continue;
  203. ttm_bo_move_to_lru_tail(&entry->bo->tbo);
  204. amdgpu_vm_move_level_in_lru(entry);
  205. }
  206. }
  207. /**
  208. * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
  209. *
  210. * @adev: amdgpu device instance
  211. * @vm: vm providing the BOs
  212. *
  213. * Move the PT BOs to the tail of the LRU.
  214. */
  215. void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
  216. struct amdgpu_vm *vm)
  217. {
  218. struct ttm_bo_global *glob = adev->mman.bdev.glob;
  219. spin_lock(&glob->lru_lock);
  220. amdgpu_vm_move_level_in_lru(&vm->root);
  221. spin_unlock(&glob->lru_lock);
  222. }
  223. /**
  224. * amdgpu_vm_alloc_levels - allocate the PD/PT levels
  225. *
  226. * @adev: amdgpu_device pointer
  227. * @vm: requested vm
  228. * @saddr: start of the address range
  229. * @eaddr: end of the address range
  230. *
  231. * Make sure the page directories and page tables are allocated
  232. */
  233. static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
  234. struct amdgpu_vm *vm,
  235. struct amdgpu_vm_pt *parent,
  236. uint64_t saddr, uint64_t eaddr,
  237. unsigned level)
  238. {
  239. unsigned shift = (adev->vm_manager.num_level - level) *
  240. amdgpu_vm_block_size;
  241. unsigned pt_idx, from, to;
  242. int r;
  243. if (!parent->entries) {
  244. unsigned num_entries = amdgpu_vm_num_entries(adev, level);
  245. parent->entries = drm_calloc_large(num_entries,
  246. sizeof(struct amdgpu_vm_pt));
  247. if (!parent->entries)
  248. return -ENOMEM;
  249. memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
  250. }
  251. from = (saddr >> shift) % amdgpu_vm_num_entries(adev, level);
  252. to = (eaddr >> shift) % amdgpu_vm_num_entries(adev, level);
  253. if (to > parent->last_entry_used)
  254. parent->last_entry_used = to;
  255. ++level;
  256. /* walk over the address space and allocate the page tables */
  257. for (pt_idx = from; pt_idx <= to; ++pt_idx) {
  258. struct reservation_object *resv = vm->root.bo->tbo.resv;
  259. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  260. struct amdgpu_bo *pt;
  261. if (!entry->bo) {
  262. r = amdgpu_bo_create(adev,
  263. amdgpu_vm_bo_size(adev, level),
  264. AMDGPU_GPU_PAGE_SIZE, true,
  265. AMDGPU_GEM_DOMAIN_VRAM,
  266. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  267. AMDGPU_GEM_CREATE_SHADOW |
  268. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  269. AMDGPU_GEM_CREATE_VRAM_CLEARED,
  270. NULL, resv, &pt);
  271. if (r)
  272. return r;
  273. /* Keep a reference to the root directory to avoid
  274. * freeing them up in the wrong order.
  275. */
  276. pt->parent = amdgpu_bo_ref(vm->root.bo);
  277. entry->bo = pt;
  278. entry->addr = 0;
  279. }
  280. if (level < adev->vm_manager.num_level) {
  281. r = amdgpu_vm_alloc_levels(adev, vm, entry, saddr,
  282. eaddr, level);
  283. if (r)
  284. return r;
  285. }
  286. }
  287. return 0;
  288. }
  289. /**
  290. * amdgpu_vm_alloc_pts - Allocate page tables.
  291. *
  292. * @adev: amdgpu_device pointer
  293. * @vm: VM to allocate page tables for
  294. * @saddr: Start address which needs to be allocated
  295. * @size: Size from start address we need.
  296. *
  297. * Make sure the page tables are allocated.
  298. */
  299. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  300. struct amdgpu_vm *vm,
  301. uint64_t saddr, uint64_t size)
  302. {
  303. uint64_t last_pfn;
  304. uint64_t eaddr;
  305. /* validate the parameters */
  306. if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
  307. return -EINVAL;
  308. eaddr = saddr + size - 1;
  309. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  310. if (last_pfn >= adev->vm_manager.max_pfn) {
  311. dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
  312. last_pfn, adev->vm_manager.max_pfn);
  313. return -EINVAL;
  314. }
  315. saddr /= AMDGPU_GPU_PAGE_SIZE;
  316. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  317. return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
  318. }
  319. static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
  320. struct amdgpu_vm_id *id)
  321. {
  322. return id->current_gpu_reset_count !=
  323. atomic_read(&adev->gpu_reset_counter) ? true : false;
  324. }
  325. /**
  326. * amdgpu_vm_grab_id - allocate the next free VMID
  327. *
  328. * @vm: vm to allocate id for
  329. * @ring: ring we want to submit job to
  330. * @sync: sync object where we add dependencies
  331. * @fence: fence protecting ID from reuse
  332. *
  333. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  334. */
  335. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  336. struct amdgpu_sync *sync, struct dma_fence *fence,
  337. struct amdgpu_job *job)
  338. {
  339. struct amdgpu_device *adev = ring->adev;
  340. uint64_t fence_context = adev->fence_context + ring->idx;
  341. struct dma_fence *updates = sync->last_vm_update;
  342. struct amdgpu_vm_id *id, *idle;
  343. struct dma_fence **fences;
  344. unsigned i;
  345. int r = 0;
  346. fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
  347. GFP_KERNEL);
  348. if (!fences)
  349. return -ENOMEM;
  350. mutex_lock(&adev->vm_manager.lock);
  351. /* Check if we have an idle VMID */
  352. i = 0;
  353. list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
  354. fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
  355. if (!fences[i])
  356. break;
  357. ++i;
  358. }
  359. /* If we can't find a idle VMID to use, wait till one becomes available */
  360. if (&idle->list == &adev->vm_manager.ids_lru) {
  361. u64 fence_context = adev->vm_manager.fence_context + ring->idx;
  362. unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
  363. struct dma_fence_array *array;
  364. unsigned j;
  365. for (j = 0; j < i; ++j)
  366. dma_fence_get(fences[j]);
  367. array = dma_fence_array_create(i, fences, fence_context,
  368. seqno, true);
  369. if (!array) {
  370. for (j = 0; j < i; ++j)
  371. dma_fence_put(fences[j]);
  372. kfree(fences);
  373. r = -ENOMEM;
  374. goto error;
  375. }
  376. r = amdgpu_sync_fence(ring->adev, sync, &array->base);
  377. dma_fence_put(&array->base);
  378. if (r)
  379. goto error;
  380. mutex_unlock(&adev->vm_manager.lock);
  381. return 0;
  382. }
  383. kfree(fences);
  384. job->vm_needs_flush = true;
  385. /* Check if we can use a VMID already assigned to this VM */
  386. i = ring->idx;
  387. do {
  388. struct dma_fence *flushed;
  389. id = vm->ids[i++];
  390. if (i == AMDGPU_MAX_RINGS)
  391. i = 0;
  392. /* Check all the prerequisites to using this VMID */
  393. if (!id)
  394. continue;
  395. if (amdgpu_vm_is_gpu_reset(adev, id))
  396. continue;
  397. if (atomic64_read(&id->owner) != vm->client_id)
  398. continue;
  399. if (job->vm_pd_addr != id->pd_gpu_addr)
  400. continue;
  401. if (!id->last_flush)
  402. continue;
  403. if (id->last_flush->context != fence_context &&
  404. !dma_fence_is_signaled(id->last_flush))
  405. continue;
  406. flushed = id->flushed_updates;
  407. if (updates &&
  408. (!flushed || dma_fence_is_later(updates, flushed)))
  409. continue;
  410. /* Good we can use this VMID. Remember this submission as
  411. * user of the VMID.
  412. */
  413. r = amdgpu_sync_fence(ring->adev, &id->active, fence);
  414. if (r)
  415. goto error;
  416. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  417. list_move_tail(&id->list, &adev->vm_manager.ids_lru);
  418. vm->ids[ring->idx] = id;
  419. job->vm_id = id - adev->vm_manager.ids;
  420. job->vm_needs_flush = false;
  421. trace_amdgpu_vm_grab_id(vm, ring->idx, job);
  422. mutex_unlock(&adev->vm_manager.lock);
  423. return 0;
  424. } while (i != ring->idx);
  425. /* Still no ID to use? Then use the idle one found earlier */
  426. id = idle;
  427. /* Remember this submission as user of the VMID */
  428. r = amdgpu_sync_fence(ring->adev, &id->active, fence);
  429. if (r)
  430. goto error;
  431. dma_fence_put(id->first);
  432. id->first = dma_fence_get(fence);
  433. dma_fence_put(id->last_flush);
  434. id->last_flush = NULL;
  435. dma_fence_put(id->flushed_updates);
  436. id->flushed_updates = dma_fence_get(updates);
  437. id->pd_gpu_addr = job->vm_pd_addr;
  438. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  439. list_move_tail(&id->list, &adev->vm_manager.ids_lru);
  440. atomic64_set(&id->owner, vm->client_id);
  441. vm->ids[ring->idx] = id;
  442. job->vm_id = id - adev->vm_manager.ids;
  443. trace_amdgpu_vm_grab_id(vm, ring->idx, job);
  444. error:
  445. mutex_unlock(&adev->vm_manager.lock);
  446. return r;
  447. }
  448. static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
  449. {
  450. struct amdgpu_device *adev = ring->adev;
  451. const struct amdgpu_ip_block *ip_block;
  452. if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
  453. /* only compute rings */
  454. return false;
  455. ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
  456. if (!ip_block)
  457. return false;
  458. if (ip_block->version->major <= 7) {
  459. /* gfx7 has no workaround */
  460. return true;
  461. } else if (ip_block->version->major == 8) {
  462. if (adev->gfx.mec_fw_version >= 673)
  463. /* gfx8 is fixed in MEC firmware 673 */
  464. return false;
  465. else
  466. return true;
  467. }
  468. return false;
  469. }
  470. static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr)
  471. {
  472. u64 addr = mc_addr;
  473. if (adev->mc.mc_funcs && adev->mc.mc_funcs->adjust_mc_addr)
  474. addr = adev->mc.mc_funcs->adjust_mc_addr(adev, addr);
  475. return addr;
  476. }
  477. /**
  478. * amdgpu_vm_flush - hardware flush the vm
  479. *
  480. * @ring: ring to use for flush
  481. * @vm_id: vmid number to use
  482. * @pd_addr: address of the page directory
  483. *
  484. * Emit a VM flush when it is necessary.
  485. */
  486. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
  487. {
  488. struct amdgpu_device *adev = ring->adev;
  489. struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
  490. bool gds_switch_needed = ring->funcs->emit_gds_switch && (
  491. id->gds_base != job->gds_base ||
  492. id->gds_size != job->gds_size ||
  493. id->gws_base != job->gws_base ||
  494. id->gws_size != job->gws_size ||
  495. id->oa_base != job->oa_base ||
  496. id->oa_size != job->oa_size);
  497. int r;
  498. if (job->vm_needs_flush || gds_switch_needed ||
  499. amdgpu_vm_is_gpu_reset(adev, id) ||
  500. amdgpu_vm_ring_has_compute_vm_bug(ring)) {
  501. unsigned patch_offset = 0;
  502. if (ring->funcs->init_cond_exec)
  503. patch_offset = amdgpu_ring_init_cond_exec(ring);
  504. if (ring->funcs->emit_pipeline_sync &&
  505. (job->vm_needs_flush || gds_switch_needed ||
  506. amdgpu_vm_ring_has_compute_vm_bug(ring)))
  507. amdgpu_ring_emit_pipeline_sync(ring);
  508. if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
  509. amdgpu_vm_is_gpu_reset(adev, id))) {
  510. struct dma_fence *fence;
  511. u64 pd_addr = amdgpu_vm_adjust_mc_addr(adev, job->vm_pd_addr);
  512. trace_amdgpu_vm_flush(pd_addr, ring->idx, job->vm_id);
  513. amdgpu_ring_emit_vm_flush(ring, job->vm_id, pd_addr);
  514. r = amdgpu_fence_emit(ring, &fence);
  515. if (r)
  516. return r;
  517. mutex_lock(&adev->vm_manager.lock);
  518. dma_fence_put(id->last_flush);
  519. id->last_flush = fence;
  520. mutex_unlock(&adev->vm_manager.lock);
  521. }
  522. if (gds_switch_needed) {
  523. id->gds_base = job->gds_base;
  524. id->gds_size = job->gds_size;
  525. id->gws_base = job->gws_base;
  526. id->gws_size = job->gws_size;
  527. id->oa_base = job->oa_base;
  528. id->oa_size = job->oa_size;
  529. amdgpu_ring_emit_gds_switch(ring, job->vm_id,
  530. job->gds_base, job->gds_size,
  531. job->gws_base, job->gws_size,
  532. job->oa_base, job->oa_size);
  533. }
  534. if (ring->funcs->patch_cond_exec)
  535. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  536. /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
  537. if (ring->funcs->emit_switch_buffer) {
  538. amdgpu_ring_emit_switch_buffer(ring);
  539. amdgpu_ring_emit_switch_buffer(ring);
  540. }
  541. }
  542. return 0;
  543. }
  544. /**
  545. * amdgpu_vm_reset_id - reset VMID to zero
  546. *
  547. * @adev: amdgpu device structure
  548. * @vm_id: vmid number to use
  549. *
  550. * Reset saved GDW, GWS and OA to force switch on next flush.
  551. */
  552. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
  553. {
  554. struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
  555. id->gds_base = 0;
  556. id->gds_size = 0;
  557. id->gws_base = 0;
  558. id->gws_size = 0;
  559. id->oa_base = 0;
  560. id->oa_size = 0;
  561. }
  562. /**
  563. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  564. *
  565. * @vm: requested vm
  566. * @bo: requested buffer object
  567. *
  568. * Find @bo inside the requested vm.
  569. * Search inside the @bos vm list for the requested vm
  570. * Returns the found bo_va or NULL if none is found
  571. *
  572. * Object has to be reserved!
  573. */
  574. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  575. struct amdgpu_bo *bo)
  576. {
  577. struct amdgpu_bo_va *bo_va;
  578. list_for_each_entry(bo_va, &bo->va, bo_list) {
  579. if (bo_va->vm == vm) {
  580. return bo_va;
  581. }
  582. }
  583. return NULL;
  584. }
  585. /**
  586. * amdgpu_vm_do_set_ptes - helper to call the right asic function
  587. *
  588. * @params: see amdgpu_pte_update_params definition
  589. * @pe: addr of the page entry
  590. * @addr: dst addr to write into pe
  591. * @count: number of page entries to update
  592. * @incr: increase next addr by incr bytes
  593. * @flags: hw access flags
  594. *
  595. * Traces the parameters and calls the right asic functions
  596. * to setup the page table using the DMA.
  597. */
  598. static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
  599. uint64_t pe, uint64_t addr,
  600. unsigned count, uint32_t incr,
  601. uint64_t flags)
  602. {
  603. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  604. if (count < 3) {
  605. amdgpu_vm_write_pte(params->adev, params->ib, pe,
  606. addr | flags, count, incr);
  607. } else {
  608. amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
  609. count, incr, flags);
  610. }
  611. }
  612. /**
  613. * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
  614. *
  615. * @params: see amdgpu_pte_update_params definition
  616. * @pe: addr of the page entry
  617. * @addr: dst addr to write into pe
  618. * @count: number of page entries to update
  619. * @incr: increase next addr by incr bytes
  620. * @flags: hw access flags
  621. *
  622. * Traces the parameters and calls the DMA function to copy the PTEs.
  623. */
  624. static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
  625. uint64_t pe, uint64_t addr,
  626. unsigned count, uint32_t incr,
  627. uint64_t flags)
  628. {
  629. uint64_t src = (params->src + (addr >> 12) * 8);
  630. trace_amdgpu_vm_copy_ptes(pe, src, count);
  631. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
  632. }
  633. /**
  634. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  635. *
  636. * @pages_addr: optional DMA address to use for lookup
  637. * @addr: the unmapped addr
  638. *
  639. * Look up the physical address of the page that the pte resolves
  640. * to and return the pointer for the page table entry.
  641. */
  642. static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  643. {
  644. uint64_t result;
  645. /* page table offset */
  646. result = pages_addr[addr >> PAGE_SHIFT];
  647. /* in case cpu page size != gpu page size*/
  648. result |= addr & (~PAGE_MASK);
  649. result &= 0xFFFFFFFFFFFFF000ULL;
  650. return result;
  651. }
  652. /*
  653. * amdgpu_vm_update_level - update a single level in the hierarchy
  654. *
  655. * @adev: amdgpu_device pointer
  656. * @vm: requested vm
  657. * @parent: parent directory
  658. *
  659. * Makes sure all entries in @parent are up to date.
  660. * Returns 0 for success, error for failure.
  661. */
  662. static int amdgpu_vm_update_level(struct amdgpu_device *adev,
  663. struct amdgpu_vm *vm,
  664. struct amdgpu_vm_pt *parent,
  665. unsigned level)
  666. {
  667. struct amdgpu_bo *shadow;
  668. struct amdgpu_ring *ring;
  669. uint64_t pd_addr, shadow_addr;
  670. uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
  671. uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
  672. unsigned count = 0, pt_idx, ndw;
  673. struct amdgpu_job *job;
  674. struct amdgpu_pte_update_params params;
  675. struct dma_fence *fence = NULL;
  676. int r;
  677. if (!parent->entries)
  678. return 0;
  679. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  680. /* padding, etc. */
  681. ndw = 64;
  682. /* assume the worst case */
  683. ndw += parent->last_entry_used * 6;
  684. pd_addr = amdgpu_bo_gpu_offset(parent->bo);
  685. shadow = parent->bo->shadow;
  686. if (shadow) {
  687. r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
  688. if (r)
  689. return r;
  690. shadow_addr = amdgpu_bo_gpu_offset(shadow);
  691. ndw *= 2;
  692. } else {
  693. shadow_addr = 0;
  694. }
  695. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  696. if (r)
  697. return r;
  698. memset(&params, 0, sizeof(params));
  699. params.adev = adev;
  700. params.ib = &job->ibs[0];
  701. /* walk over the address space and update the directory */
  702. for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
  703. struct amdgpu_bo *bo = parent->entries[pt_idx].bo;
  704. uint64_t pde, pt;
  705. if (bo == NULL)
  706. continue;
  707. if (bo->shadow) {
  708. struct amdgpu_bo *pt_shadow = bo->shadow;
  709. r = amdgpu_ttm_bind(&pt_shadow->tbo,
  710. &pt_shadow->tbo.mem);
  711. if (r)
  712. return r;
  713. }
  714. pt = amdgpu_bo_gpu_offset(bo);
  715. if (parent->entries[pt_idx].addr == pt)
  716. continue;
  717. parent->entries[pt_idx].addr = pt;
  718. pde = pd_addr + pt_idx * 8;
  719. if (((last_pde + 8 * count) != pde) ||
  720. ((last_pt + incr * count) != pt) ||
  721. (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
  722. if (count) {
  723. uint64_t pt_addr =
  724. amdgpu_vm_adjust_mc_addr(adev, last_pt);
  725. if (shadow)
  726. amdgpu_vm_do_set_ptes(&params,
  727. last_shadow,
  728. pt_addr, count,
  729. incr,
  730. AMDGPU_PTE_VALID);
  731. amdgpu_vm_do_set_ptes(&params, last_pde,
  732. pt_addr, count, incr,
  733. AMDGPU_PTE_VALID);
  734. }
  735. count = 1;
  736. last_pde = pde;
  737. last_shadow = shadow_addr + pt_idx * 8;
  738. last_pt = pt;
  739. } else {
  740. ++count;
  741. }
  742. }
  743. if (count) {
  744. uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt);
  745. if (vm->root.bo->shadow)
  746. amdgpu_vm_do_set_ptes(&params, last_shadow, pt_addr,
  747. count, incr, AMDGPU_PTE_VALID);
  748. amdgpu_vm_do_set_ptes(&params, last_pde, pt_addr,
  749. count, incr, AMDGPU_PTE_VALID);
  750. }
  751. if (params.ib->length_dw == 0) {
  752. amdgpu_job_free(job);
  753. } else {
  754. amdgpu_ring_pad_ib(ring, params.ib);
  755. amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv,
  756. AMDGPU_FENCE_OWNER_VM);
  757. if (shadow)
  758. amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
  759. AMDGPU_FENCE_OWNER_VM);
  760. WARN_ON(params.ib->length_dw > ndw);
  761. r = amdgpu_job_submit(job, ring, &vm->entity,
  762. AMDGPU_FENCE_OWNER_VM, &fence);
  763. if (r)
  764. goto error_free;
  765. amdgpu_bo_fence(parent->bo, fence, true);
  766. dma_fence_put(vm->last_dir_update);
  767. vm->last_dir_update = dma_fence_get(fence);
  768. dma_fence_put(fence);
  769. }
  770. /*
  771. * Recurse into the subdirectories. This recursion is harmless because
  772. * we only have a maximum of 5 layers.
  773. */
  774. for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
  775. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  776. if (!entry->bo)
  777. continue;
  778. r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
  779. if (r)
  780. return r;
  781. }
  782. return 0;
  783. error_free:
  784. amdgpu_job_free(job);
  785. return r;
  786. }
  787. /*
  788. * amdgpu_vm_update_directories - make sure that all directories are valid
  789. *
  790. * @adev: amdgpu_device pointer
  791. * @vm: requested vm
  792. *
  793. * Makes sure all directories are up to date.
  794. * Returns 0 for success, error for failure.
  795. */
  796. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  797. struct amdgpu_vm *vm)
  798. {
  799. return amdgpu_vm_update_level(adev, vm, &vm->root, 0);
  800. }
  801. /**
  802. * amdgpu_vm_find_pt - find the page table for an address
  803. *
  804. * @p: see amdgpu_pte_update_params definition
  805. * @addr: virtual address in question
  806. *
  807. * Find the page table BO for a virtual address, return NULL when none found.
  808. */
  809. static struct amdgpu_bo *amdgpu_vm_get_pt(struct amdgpu_pte_update_params *p,
  810. uint64_t addr)
  811. {
  812. struct amdgpu_vm_pt *entry = &p->vm->root;
  813. unsigned idx, level = p->adev->vm_manager.num_level;
  814. while (entry->entries) {
  815. idx = addr >> (amdgpu_vm_block_size * level--);
  816. idx %= amdgpu_bo_size(entry->bo) / 8;
  817. entry = &entry->entries[idx];
  818. }
  819. if (level)
  820. return NULL;
  821. return entry->bo;
  822. }
  823. /**
  824. * amdgpu_vm_update_ptes - make sure that page tables are valid
  825. *
  826. * @params: see amdgpu_pte_update_params definition
  827. * @vm: requested vm
  828. * @start: start of GPU address range
  829. * @end: end of GPU address range
  830. * @dst: destination address to map to, the next dst inside the function
  831. * @flags: mapping flags
  832. *
  833. * Update the page tables in the range @start - @end.
  834. */
  835. static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  836. uint64_t start, uint64_t end,
  837. uint64_t dst, uint64_t flags)
  838. {
  839. const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  840. uint64_t cur_pe_start, cur_nptes, cur_dst;
  841. uint64_t addr; /* next GPU address to be updated */
  842. struct amdgpu_bo *pt;
  843. unsigned nptes; /* next number of ptes to be updated */
  844. uint64_t next_pe_start;
  845. /* initialize the variables */
  846. addr = start;
  847. pt = amdgpu_vm_get_pt(params, addr);
  848. if (!pt)
  849. return;
  850. if (params->shadow) {
  851. if (!pt->shadow)
  852. return;
  853. pt = pt->shadow;
  854. }
  855. if ((addr & ~mask) == (end & ~mask))
  856. nptes = end - addr;
  857. else
  858. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  859. cur_pe_start = amdgpu_bo_gpu_offset(pt);
  860. cur_pe_start += (addr & mask) * 8;
  861. cur_nptes = nptes;
  862. cur_dst = dst;
  863. /* for next ptb*/
  864. addr += nptes;
  865. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  866. /* walk over the address space and update the page tables */
  867. while (addr < end) {
  868. pt = amdgpu_vm_get_pt(params, addr);
  869. if (!pt)
  870. return;
  871. if (params->shadow) {
  872. if (!pt->shadow)
  873. return;
  874. pt = pt->shadow;
  875. }
  876. if ((addr & ~mask) == (end & ~mask))
  877. nptes = end - addr;
  878. else
  879. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  880. next_pe_start = amdgpu_bo_gpu_offset(pt);
  881. next_pe_start += (addr & mask) * 8;
  882. if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
  883. ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
  884. /* The next ptb is consecutive to current ptb.
  885. * Don't call the update function now.
  886. * Will update two ptbs together in future.
  887. */
  888. cur_nptes += nptes;
  889. } else {
  890. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  891. AMDGPU_GPU_PAGE_SIZE, flags);
  892. cur_pe_start = next_pe_start;
  893. cur_nptes = nptes;
  894. cur_dst = dst;
  895. }
  896. /* for next ptb*/
  897. addr += nptes;
  898. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  899. }
  900. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  901. AMDGPU_GPU_PAGE_SIZE, flags);
  902. }
  903. /*
  904. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  905. *
  906. * @params: see amdgpu_pte_update_params definition
  907. * @vm: requested vm
  908. * @start: first PTE to handle
  909. * @end: last PTE to handle
  910. * @dst: addr those PTEs should point to
  911. * @flags: hw mapping flags
  912. */
  913. static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  914. uint64_t start, uint64_t end,
  915. uint64_t dst, uint64_t flags)
  916. {
  917. /**
  918. * The MC L1 TLB supports variable sized pages, based on a fragment
  919. * field in the PTE. When this field is set to a non-zero value, page
  920. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  921. * flags are considered valid for all PTEs within the fragment range
  922. * and corresponding mappings are assumed to be physically contiguous.
  923. *
  924. * The L1 TLB can store a single PTE for the whole fragment,
  925. * significantly increasing the space available for translation
  926. * caching. This leads to large improvements in throughput when the
  927. * TLB is under pressure.
  928. *
  929. * The L2 TLB distributes small and large fragments into two
  930. * asymmetric partitions. The large fragment cache is significantly
  931. * larger. Thus, we try to use large fragments wherever possible.
  932. * Userspace can support this by aligning virtual base address and
  933. * allocation size to the fragment size.
  934. */
  935. /* SI and newer are optimized for 64KB */
  936. uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
  937. uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
  938. uint64_t frag_start = ALIGN(start, frag_align);
  939. uint64_t frag_end = end & ~(frag_align - 1);
  940. /* system pages are non continuously */
  941. if (params->src || !(flags & AMDGPU_PTE_VALID) ||
  942. (frag_start >= frag_end)) {
  943. amdgpu_vm_update_ptes(params, start, end, dst, flags);
  944. return;
  945. }
  946. /* handle the 4K area at the beginning */
  947. if (start != frag_start) {
  948. amdgpu_vm_update_ptes(params, start, frag_start,
  949. dst, flags);
  950. dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
  951. }
  952. /* handle the area in the middle */
  953. amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
  954. flags | frag_flags);
  955. /* handle the 4K area at the end */
  956. if (frag_end != end) {
  957. dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
  958. amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
  959. }
  960. }
  961. /**
  962. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  963. *
  964. * @adev: amdgpu_device pointer
  965. * @exclusive: fence we need to sync to
  966. * @src: address where to copy page table entries from
  967. * @pages_addr: DMA addresses to use for mapping
  968. * @vm: requested vm
  969. * @start: start of mapped range
  970. * @last: last mapped entry
  971. * @flags: flags for the entries
  972. * @addr: addr to set the area to
  973. * @fence: optional resulting fence
  974. *
  975. * Fill in the page table entries between @start and @last.
  976. * Returns 0 for success, -EINVAL for failure.
  977. */
  978. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  979. struct dma_fence *exclusive,
  980. uint64_t src,
  981. dma_addr_t *pages_addr,
  982. struct amdgpu_vm *vm,
  983. uint64_t start, uint64_t last,
  984. uint64_t flags, uint64_t addr,
  985. struct dma_fence **fence)
  986. {
  987. struct amdgpu_ring *ring;
  988. void *owner = AMDGPU_FENCE_OWNER_VM;
  989. unsigned nptes, ncmds, ndw;
  990. struct amdgpu_job *job;
  991. struct amdgpu_pte_update_params params;
  992. struct dma_fence *f = NULL;
  993. int r;
  994. memset(&params, 0, sizeof(params));
  995. params.adev = adev;
  996. params.vm = vm;
  997. params.src = src;
  998. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  999. /* sync to everything on unmapping */
  1000. if (!(flags & AMDGPU_PTE_VALID))
  1001. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  1002. nptes = last - start + 1;
  1003. /*
  1004. * reserve space for one command every (1 << BLOCK_SIZE)
  1005. * entries or 2k dwords (whatever is smaller)
  1006. */
  1007. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  1008. /* padding, etc. */
  1009. ndw = 64;
  1010. if (src) {
  1011. /* only copy commands needed */
  1012. ndw += ncmds * 7;
  1013. params.func = amdgpu_vm_do_copy_ptes;
  1014. } else if (pages_addr) {
  1015. /* copy commands needed */
  1016. ndw += ncmds * 7;
  1017. /* and also PTEs */
  1018. ndw += nptes * 2;
  1019. params.func = amdgpu_vm_do_copy_ptes;
  1020. } else {
  1021. /* set page commands needed */
  1022. ndw += ncmds * 10;
  1023. /* two extra commands for begin/end of fragment */
  1024. ndw += 2 * 10;
  1025. params.func = amdgpu_vm_do_set_ptes;
  1026. }
  1027. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  1028. if (r)
  1029. return r;
  1030. params.ib = &job->ibs[0];
  1031. if (!src && pages_addr) {
  1032. uint64_t *pte;
  1033. unsigned i;
  1034. /* Put the PTEs at the end of the IB. */
  1035. i = ndw - nptes * 2;
  1036. pte= (uint64_t *)&(job->ibs->ptr[i]);
  1037. params.src = job->ibs->gpu_addr + i * 4;
  1038. for (i = 0; i < nptes; ++i) {
  1039. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  1040. AMDGPU_GPU_PAGE_SIZE);
  1041. pte[i] |= flags;
  1042. }
  1043. addr = 0;
  1044. }
  1045. r = amdgpu_sync_fence(adev, &job->sync, exclusive);
  1046. if (r)
  1047. goto error_free;
  1048. r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
  1049. owner);
  1050. if (r)
  1051. goto error_free;
  1052. r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
  1053. if (r)
  1054. goto error_free;
  1055. params.shadow = true;
  1056. amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1057. params.shadow = false;
  1058. amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1059. amdgpu_ring_pad_ib(ring, params.ib);
  1060. WARN_ON(params.ib->length_dw > ndw);
  1061. r = amdgpu_job_submit(job, ring, &vm->entity,
  1062. AMDGPU_FENCE_OWNER_VM, &f);
  1063. if (r)
  1064. goto error_free;
  1065. amdgpu_bo_fence(vm->root.bo, f, true);
  1066. dma_fence_put(*fence);
  1067. *fence = f;
  1068. return 0;
  1069. error_free:
  1070. amdgpu_job_free(job);
  1071. return r;
  1072. }
  1073. /**
  1074. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  1075. *
  1076. * @adev: amdgpu_device pointer
  1077. * @exclusive: fence we need to sync to
  1078. * @gtt_flags: flags as they are used for GTT
  1079. * @pages_addr: DMA addresses to use for mapping
  1080. * @vm: requested vm
  1081. * @mapping: mapped range and flags to use for the update
  1082. * @flags: HW flags for the mapping
  1083. * @nodes: array of drm_mm_nodes with the MC addresses
  1084. * @fence: optional resulting fence
  1085. *
  1086. * Split the mapping into smaller chunks so that each update fits
  1087. * into a SDMA IB.
  1088. * Returns 0 for success, -EINVAL for failure.
  1089. */
  1090. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  1091. struct dma_fence *exclusive,
  1092. uint64_t gtt_flags,
  1093. dma_addr_t *pages_addr,
  1094. struct amdgpu_vm *vm,
  1095. struct amdgpu_bo_va_mapping *mapping,
  1096. uint64_t flags,
  1097. struct drm_mm_node *nodes,
  1098. struct dma_fence **fence)
  1099. {
  1100. uint64_t pfn, src = 0, start = mapping->it.start;
  1101. int r;
  1102. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  1103. * but in case of something, we filter the flags in first place
  1104. */
  1105. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  1106. flags &= ~AMDGPU_PTE_READABLE;
  1107. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  1108. flags &= ~AMDGPU_PTE_WRITEABLE;
  1109. flags &= ~AMDGPU_PTE_EXECUTABLE;
  1110. flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
  1111. flags &= ~AMDGPU_PTE_MTYPE_MASK;
  1112. flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
  1113. trace_amdgpu_vm_bo_update(mapping);
  1114. pfn = mapping->offset >> PAGE_SHIFT;
  1115. if (nodes) {
  1116. while (pfn >= nodes->size) {
  1117. pfn -= nodes->size;
  1118. ++nodes;
  1119. }
  1120. }
  1121. do {
  1122. uint64_t max_entries;
  1123. uint64_t addr, last;
  1124. if (nodes) {
  1125. addr = nodes->start << PAGE_SHIFT;
  1126. max_entries = (nodes->size - pfn) *
  1127. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  1128. } else {
  1129. addr = 0;
  1130. max_entries = S64_MAX;
  1131. }
  1132. if (pages_addr) {
  1133. if (flags == gtt_flags)
  1134. src = adev->gart.table_addr +
  1135. (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
  1136. else
  1137. max_entries = min(max_entries, 16ull * 1024ull);
  1138. addr = 0;
  1139. } else if (flags & AMDGPU_PTE_VALID) {
  1140. addr += adev->vm_manager.vram_base_offset;
  1141. }
  1142. addr += pfn << PAGE_SHIFT;
  1143. last = min((uint64_t)mapping->it.last, start + max_entries - 1);
  1144. r = amdgpu_vm_bo_update_mapping(adev, exclusive,
  1145. src, pages_addr, vm,
  1146. start, last, flags, addr,
  1147. fence);
  1148. if (r)
  1149. return r;
  1150. pfn += last - start + 1;
  1151. if (nodes && nodes->size == pfn) {
  1152. pfn = 0;
  1153. ++nodes;
  1154. }
  1155. start = last + 1;
  1156. } while (unlikely(start != mapping->it.last + 1));
  1157. return 0;
  1158. }
  1159. /**
  1160. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  1161. *
  1162. * @adev: amdgpu_device pointer
  1163. * @bo_va: requested BO and VM object
  1164. * @clear: if true clear the entries
  1165. *
  1166. * Fill in the page table entries for @bo_va.
  1167. * Returns 0 for success, -EINVAL for failure.
  1168. */
  1169. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  1170. struct amdgpu_bo_va *bo_va,
  1171. bool clear)
  1172. {
  1173. struct amdgpu_vm *vm = bo_va->vm;
  1174. struct amdgpu_bo_va_mapping *mapping;
  1175. dma_addr_t *pages_addr = NULL;
  1176. uint64_t gtt_flags, flags;
  1177. struct ttm_mem_reg *mem;
  1178. struct drm_mm_node *nodes;
  1179. struct dma_fence *exclusive;
  1180. int r;
  1181. if (clear || !bo_va->bo) {
  1182. mem = NULL;
  1183. nodes = NULL;
  1184. exclusive = NULL;
  1185. } else {
  1186. struct ttm_dma_tt *ttm;
  1187. mem = &bo_va->bo->tbo.mem;
  1188. nodes = mem->mm_node;
  1189. if (mem->mem_type == TTM_PL_TT) {
  1190. ttm = container_of(bo_va->bo->tbo.ttm, struct
  1191. ttm_dma_tt, ttm);
  1192. pages_addr = ttm->dma_address;
  1193. }
  1194. exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
  1195. }
  1196. if (bo_va->bo) {
  1197. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  1198. gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
  1199. adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
  1200. flags : 0;
  1201. } else {
  1202. flags = 0x0;
  1203. gtt_flags = ~0x0;
  1204. }
  1205. spin_lock(&vm->status_lock);
  1206. if (!list_empty(&bo_va->vm_status))
  1207. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1208. spin_unlock(&vm->status_lock);
  1209. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1210. r = amdgpu_vm_bo_split_mapping(adev, exclusive,
  1211. gtt_flags, pages_addr, vm,
  1212. mapping, flags, nodes,
  1213. &bo_va->last_pt_update);
  1214. if (r)
  1215. return r;
  1216. }
  1217. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1218. list_for_each_entry(mapping, &bo_va->valids, list)
  1219. trace_amdgpu_vm_bo_mapping(mapping);
  1220. list_for_each_entry(mapping, &bo_va->invalids, list)
  1221. trace_amdgpu_vm_bo_mapping(mapping);
  1222. }
  1223. spin_lock(&vm->status_lock);
  1224. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1225. list_del_init(&bo_va->vm_status);
  1226. if (clear)
  1227. list_add(&bo_va->vm_status, &vm->cleared);
  1228. spin_unlock(&vm->status_lock);
  1229. return 0;
  1230. }
  1231. /**
  1232. * amdgpu_vm_update_prt_state - update the global PRT state
  1233. */
  1234. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1235. {
  1236. unsigned long flags;
  1237. bool enable;
  1238. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1239. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1240. adev->gart.gart_funcs->set_prt(adev, enable);
  1241. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1242. }
  1243. /**
  1244. * amdgpu_vm_prt_get - add a PRT user
  1245. */
  1246. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1247. {
  1248. if (!adev->gart.gart_funcs->set_prt)
  1249. return;
  1250. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1251. amdgpu_vm_update_prt_state(adev);
  1252. }
  1253. /**
  1254. * amdgpu_vm_prt_put - drop a PRT user
  1255. */
  1256. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1257. {
  1258. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1259. amdgpu_vm_update_prt_state(adev);
  1260. }
  1261. /**
  1262. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1263. */
  1264. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1265. {
  1266. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1267. amdgpu_vm_prt_put(cb->adev);
  1268. kfree(cb);
  1269. }
  1270. /**
  1271. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1272. */
  1273. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1274. struct dma_fence *fence)
  1275. {
  1276. struct amdgpu_prt_cb *cb;
  1277. if (!adev->gart.gart_funcs->set_prt)
  1278. return;
  1279. cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
  1280. if (!cb) {
  1281. /* Last resort when we are OOM */
  1282. if (fence)
  1283. dma_fence_wait(fence, false);
  1284. amdgpu_vm_prt_put(cb->adev);
  1285. } else {
  1286. cb->adev = adev;
  1287. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1288. amdgpu_vm_prt_cb))
  1289. amdgpu_vm_prt_cb(fence, &cb->cb);
  1290. }
  1291. }
  1292. /**
  1293. * amdgpu_vm_free_mapping - free a mapping
  1294. *
  1295. * @adev: amdgpu_device pointer
  1296. * @vm: requested vm
  1297. * @mapping: mapping to be freed
  1298. * @fence: fence of the unmap operation
  1299. *
  1300. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1301. */
  1302. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1303. struct amdgpu_vm *vm,
  1304. struct amdgpu_bo_va_mapping *mapping,
  1305. struct dma_fence *fence)
  1306. {
  1307. if (mapping->flags & AMDGPU_PTE_PRT)
  1308. amdgpu_vm_add_prt_cb(adev, fence);
  1309. kfree(mapping);
  1310. }
  1311. /**
  1312. * amdgpu_vm_prt_fini - finish all prt mappings
  1313. *
  1314. * @adev: amdgpu_device pointer
  1315. * @vm: requested vm
  1316. *
  1317. * Register a cleanup callback to disable PRT support after VM dies.
  1318. */
  1319. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1320. {
  1321. struct reservation_object *resv = vm->root.bo->tbo.resv;
  1322. struct dma_fence *excl, **shared;
  1323. unsigned i, shared_count;
  1324. int r;
  1325. r = reservation_object_get_fences_rcu(resv, &excl,
  1326. &shared_count, &shared);
  1327. if (r) {
  1328. /* Not enough memory to grab the fence list, as last resort
  1329. * block for all the fences to complete.
  1330. */
  1331. reservation_object_wait_timeout_rcu(resv, true, false,
  1332. MAX_SCHEDULE_TIMEOUT);
  1333. return;
  1334. }
  1335. /* Add a callback for each fence in the reservation object */
  1336. amdgpu_vm_prt_get(adev);
  1337. amdgpu_vm_add_prt_cb(adev, excl);
  1338. for (i = 0; i < shared_count; ++i) {
  1339. amdgpu_vm_prt_get(adev);
  1340. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1341. }
  1342. kfree(shared);
  1343. }
  1344. /**
  1345. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1346. *
  1347. * @adev: amdgpu_device pointer
  1348. * @vm: requested vm
  1349. * @fence: optional resulting fence (unchanged if no work needed to be done
  1350. * or if an error occurred)
  1351. *
  1352. * Make sure all freed BOs are cleared in the PT.
  1353. * Returns 0 for success.
  1354. *
  1355. * PTs have to be reserved and mutex must be locked!
  1356. */
  1357. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1358. struct amdgpu_vm *vm,
  1359. struct dma_fence **fence)
  1360. {
  1361. struct amdgpu_bo_va_mapping *mapping;
  1362. struct dma_fence *f = NULL;
  1363. int r;
  1364. while (!list_empty(&vm->freed)) {
  1365. mapping = list_first_entry(&vm->freed,
  1366. struct amdgpu_bo_va_mapping, list);
  1367. list_del(&mapping->list);
  1368. r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
  1369. 0, 0, &f);
  1370. amdgpu_vm_free_mapping(adev, vm, mapping, f);
  1371. if (r) {
  1372. dma_fence_put(f);
  1373. return r;
  1374. }
  1375. }
  1376. if (fence && f) {
  1377. dma_fence_put(*fence);
  1378. *fence = f;
  1379. } else {
  1380. dma_fence_put(f);
  1381. }
  1382. return 0;
  1383. }
  1384. /**
  1385. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  1386. *
  1387. * @adev: amdgpu_device pointer
  1388. * @vm: requested vm
  1389. *
  1390. * Make sure all invalidated BOs are cleared in the PT.
  1391. * Returns 0 for success.
  1392. *
  1393. * PTs have to be reserved and mutex must be locked!
  1394. */
  1395. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  1396. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  1397. {
  1398. struct amdgpu_bo_va *bo_va = NULL;
  1399. int r = 0;
  1400. spin_lock(&vm->status_lock);
  1401. while (!list_empty(&vm->invalidated)) {
  1402. bo_va = list_first_entry(&vm->invalidated,
  1403. struct amdgpu_bo_va, vm_status);
  1404. spin_unlock(&vm->status_lock);
  1405. r = amdgpu_vm_bo_update(adev, bo_va, true);
  1406. if (r)
  1407. return r;
  1408. spin_lock(&vm->status_lock);
  1409. }
  1410. spin_unlock(&vm->status_lock);
  1411. if (bo_va)
  1412. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  1413. return r;
  1414. }
  1415. /**
  1416. * amdgpu_vm_bo_add - add a bo to a specific vm
  1417. *
  1418. * @adev: amdgpu_device pointer
  1419. * @vm: requested vm
  1420. * @bo: amdgpu buffer object
  1421. *
  1422. * Add @bo into the requested vm.
  1423. * Add @bo to the list of bos associated with the vm
  1424. * Returns newly added bo_va or NULL for failure
  1425. *
  1426. * Object has to be reserved!
  1427. */
  1428. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1429. struct amdgpu_vm *vm,
  1430. struct amdgpu_bo *bo)
  1431. {
  1432. struct amdgpu_bo_va *bo_va;
  1433. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1434. if (bo_va == NULL) {
  1435. return NULL;
  1436. }
  1437. bo_va->vm = vm;
  1438. bo_va->bo = bo;
  1439. bo_va->ref_count = 1;
  1440. INIT_LIST_HEAD(&bo_va->bo_list);
  1441. INIT_LIST_HEAD(&bo_va->valids);
  1442. INIT_LIST_HEAD(&bo_va->invalids);
  1443. INIT_LIST_HEAD(&bo_va->vm_status);
  1444. if (bo)
  1445. list_add_tail(&bo_va->bo_list, &bo->va);
  1446. return bo_va;
  1447. }
  1448. /**
  1449. * amdgpu_vm_bo_map - map bo inside a vm
  1450. *
  1451. * @adev: amdgpu_device pointer
  1452. * @bo_va: bo_va to store the address
  1453. * @saddr: where to map the BO
  1454. * @offset: requested offset in the BO
  1455. * @flags: attributes of pages (read/write/valid/etc.)
  1456. *
  1457. * Add a mapping of the BO at the specefied addr into the VM.
  1458. * Returns 0 for success, error for failure.
  1459. *
  1460. * Object has to be reserved and unreserved outside!
  1461. */
  1462. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1463. struct amdgpu_bo_va *bo_va,
  1464. uint64_t saddr, uint64_t offset,
  1465. uint64_t size, uint64_t flags)
  1466. {
  1467. struct amdgpu_bo_va_mapping *mapping;
  1468. struct amdgpu_vm *vm = bo_va->vm;
  1469. struct interval_tree_node *it;
  1470. uint64_t eaddr;
  1471. /* validate the parameters */
  1472. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1473. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1474. return -EINVAL;
  1475. /* make sure object fit at this offset */
  1476. eaddr = saddr + size - 1;
  1477. if (saddr >= eaddr ||
  1478. (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
  1479. return -EINVAL;
  1480. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1481. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1482. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  1483. if (it) {
  1484. struct amdgpu_bo_va_mapping *tmp;
  1485. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  1486. /* bo and tmp overlap, invalid addr */
  1487. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1488. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  1489. tmp->it.start, tmp->it.last + 1);
  1490. return -EINVAL;
  1491. }
  1492. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1493. if (!mapping)
  1494. return -ENOMEM;
  1495. INIT_LIST_HEAD(&mapping->list);
  1496. mapping->it.start = saddr;
  1497. mapping->it.last = eaddr;
  1498. mapping->offset = offset;
  1499. mapping->flags = flags;
  1500. list_add(&mapping->list, &bo_va->invalids);
  1501. interval_tree_insert(&mapping->it, &vm->va);
  1502. if (flags & AMDGPU_PTE_PRT)
  1503. amdgpu_vm_prt_get(adev);
  1504. return 0;
  1505. }
  1506. /**
  1507. * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  1508. *
  1509. * @adev: amdgpu_device pointer
  1510. * @bo_va: bo_va to store the address
  1511. * @saddr: where to map the BO
  1512. * @offset: requested offset in the BO
  1513. * @flags: attributes of pages (read/write/valid/etc.)
  1514. *
  1515. * Add a mapping of the BO at the specefied addr into the VM. Replace existing
  1516. * mappings as we do so.
  1517. * Returns 0 for success, error for failure.
  1518. *
  1519. * Object has to be reserved and unreserved outside!
  1520. */
  1521. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  1522. struct amdgpu_bo_va *bo_va,
  1523. uint64_t saddr, uint64_t offset,
  1524. uint64_t size, uint64_t flags)
  1525. {
  1526. struct amdgpu_bo_va_mapping *mapping;
  1527. struct amdgpu_vm *vm = bo_va->vm;
  1528. uint64_t eaddr;
  1529. int r;
  1530. /* validate the parameters */
  1531. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1532. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1533. return -EINVAL;
  1534. /* make sure object fit at this offset */
  1535. eaddr = saddr + size - 1;
  1536. if (saddr >= eaddr ||
  1537. (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
  1538. return -EINVAL;
  1539. /* Allocate all the needed memory */
  1540. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1541. if (!mapping)
  1542. return -ENOMEM;
  1543. r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
  1544. if (r) {
  1545. kfree(mapping);
  1546. return r;
  1547. }
  1548. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1549. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1550. mapping->it.start = saddr;
  1551. mapping->it.last = eaddr;
  1552. mapping->offset = offset;
  1553. mapping->flags = flags;
  1554. list_add(&mapping->list, &bo_va->invalids);
  1555. interval_tree_insert(&mapping->it, &vm->va);
  1556. if (flags & AMDGPU_PTE_PRT)
  1557. amdgpu_vm_prt_get(adev);
  1558. return 0;
  1559. }
  1560. /**
  1561. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1562. *
  1563. * @adev: amdgpu_device pointer
  1564. * @bo_va: bo_va to remove the address from
  1565. * @saddr: where to the BO is mapped
  1566. *
  1567. * Remove a mapping of the BO at the specefied addr from the VM.
  1568. * Returns 0 for success, error for failure.
  1569. *
  1570. * Object has to be reserved and unreserved outside!
  1571. */
  1572. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1573. struct amdgpu_bo_va *bo_va,
  1574. uint64_t saddr)
  1575. {
  1576. struct amdgpu_bo_va_mapping *mapping;
  1577. struct amdgpu_vm *vm = bo_va->vm;
  1578. bool valid = true;
  1579. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1580. list_for_each_entry(mapping, &bo_va->valids, list) {
  1581. if (mapping->it.start == saddr)
  1582. break;
  1583. }
  1584. if (&mapping->list == &bo_va->valids) {
  1585. valid = false;
  1586. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1587. if (mapping->it.start == saddr)
  1588. break;
  1589. }
  1590. if (&mapping->list == &bo_va->invalids)
  1591. return -ENOENT;
  1592. }
  1593. list_del(&mapping->list);
  1594. interval_tree_remove(&mapping->it, &vm->va);
  1595. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1596. if (valid)
  1597. list_add(&mapping->list, &vm->freed);
  1598. else
  1599. amdgpu_vm_free_mapping(adev, vm, mapping,
  1600. bo_va->last_pt_update);
  1601. return 0;
  1602. }
  1603. /**
  1604. * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  1605. *
  1606. * @adev: amdgpu_device pointer
  1607. * @vm: VM structure to use
  1608. * @saddr: start of the range
  1609. * @size: size of the range
  1610. *
  1611. * Remove all mappings in a range, split them as appropriate.
  1612. * Returns 0 for success, error for failure.
  1613. */
  1614. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  1615. struct amdgpu_vm *vm,
  1616. uint64_t saddr, uint64_t size)
  1617. {
  1618. struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
  1619. struct interval_tree_node *it;
  1620. LIST_HEAD(removed);
  1621. uint64_t eaddr;
  1622. eaddr = saddr + size - 1;
  1623. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1624. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1625. /* Allocate all the needed memory */
  1626. before = kzalloc(sizeof(*before), GFP_KERNEL);
  1627. if (!before)
  1628. return -ENOMEM;
  1629. INIT_LIST_HEAD(&before->list);
  1630. after = kzalloc(sizeof(*after), GFP_KERNEL);
  1631. if (!after) {
  1632. kfree(before);
  1633. return -ENOMEM;
  1634. }
  1635. INIT_LIST_HEAD(&after->list);
  1636. /* Now gather all removed mappings */
  1637. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  1638. while (it) {
  1639. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  1640. it = interval_tree_iter_next(it, saddr, eaddr);
  1641. /* Remember mapping split at the start */
  1642. if (tmp->it.start < saddr) {
  1643. before->it.start = tmp->it.start;
  1644. before->it.last = saddr - 1;
  1645. before->offset = tmp->offset;
  1646. before->flags = tmp->flags;
  1647. list_add(&before->list, &tmp->list);
  1648. }
  1649. /* Remember mapping split at the end */
  1650. if (tmp->it.last > eaddr) {
  1651. after->it.start = eaddr + 1;
  1652. after->it.last = tmp->it.last;
  1653. after->offset = tmp->offset;
  1654. after->offset += after->it.start - tmp->it.start;
  1655. after->flags = tmp->flags;
  1656. list_add(&after->list, &tmp->list);
  1657. }
  1658. list_del(&tmp->list);
  1659. list_add(&tmp->list, &removed);
  1660. }
  1661. /* And free them up */
  1662. list_for_each_entry_safe(tmp, next, &removed, list) {
  1663. interval_tree_remove(&tmp->it, &vm->va);
  1664. list_del(&tmp->list);
  1665. if (tmp->it.start < saddr)
  1666. tmp->it.start = saddr;
  1667. if (tmp->it.last > eaddr)
  1668. tmp->it.last = eaddr;
  1669. list_add(&tmp->list, &vm->freed);
  1670. trace_amdgpu_vm_bo_unmap(NULL, tmp);
  1671. }
  1672. /* Insert partial mapping before the range */
  1673. if (!list_empty(&before->list)) {
  1674. interval_tree_insert(&before->it, &vm->va);
  1675. if (before->flags & AMDGPU_PTE_PRT)
  1676. amdgpu_vm_prt_get(adev);
  1677. } else {
  1678. kfree(before);
  1679. }
  1680. /* Insert partial mapping after the range */
  1681. if (!list_empty(&after->list)) {
  1682. interval_tree_insert(&after->it, &vm->va);
  1683. if (after->flags & AMDGPU_PTE_PRT)
  1684. amdgpu_vm_prt_get(adev);
  1685. } else {
  1686. kfree(after);
  1687. }
  1688. return 0;
  1689. }
  1690. /**
  1691. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1692. *
  1693. * @adev: amdgpu_device pointer
  1694. * @bo_va: requested bo_va
  1695. *
  1696. * Remove @bo_va->bo from the requested vm.
  1697. *
  1698. * Object have to be reserved!
  1699. */
  1700. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1701. struct amdgpu_bo_va *bo_va)
  1702. {
  1703. struct amdgpu_bo_va_mapping *mapping, *next;
  1704. struct amdgpu_vm *vm = bo_va->vm;
  1705. list_del(&bo_va->bo_list);
  1706. spin_lock(&vm->status_lock);
  1707. list_del(&bo_va->vm_status);
  1708. spin_unlock(&vm->status_lock);
  1709. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1710. list_del(&mapping->list);
  1711. interval_tree_remove(&mapping->it, &vm->va);
  1712. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1713. list_add(&mapping->list, &vm->freed);
  1714. }
  1715. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1716. list_del(&mapping->list);
  1717. interval_tree_remove(&mapping->it, &vm->va);
  1718. amdgpu_vm_free_mapping(adev, vm, mapping,
  1719. bo_va->last_pt_update);
  1720. }
  1721. dma_fence_put(bo_va->last_pt_update);
  1722. kfree(bo_va);
  1723. }
  1724. /**
  1725. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1726. *
  1727. * @adev: amdgpu_device pointer
  1728. * @vm: requested vm
  1729. * @bo: amdgpu buffer object
  1730. *
  1731. * Mark @bo as invalid.
  1732. */
  1733. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1734. struct amdgpu_bo *bo)
  1735. {
  1736. struct amdgpu_bo_va *bo_va;
  1737. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1738. spin_lock(&bo_va->vm->status_lock);
  1739. if (list_empty(&bo_va->vm_status))
  1740. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1741. spin_unlock(&bo_va->vm->status_lock);
  1742. }
  1743. }
  1744. /**
  1745. * amdgpu_vm_init - initialize a vm instance
  1746. *
  1747. * @adev: amdgpu_device pointer
  1748. * @vm: requested vm
  1749. *
  1750. * Init @vm fields.
  1751. */
  1752. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1753. {
  1754. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1755. AMDGPU_VM_PTE_COUNT * 8);
  1756. unsigned ring_instance;
  1757. struct amdgpu_ring *ring;
  1758. struct amd_sched_rq *rq;
  1759. int i, r;
  1760. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1761. vm->ids[i] = NULL;
  1762. vm->va = RB_ROOT;
  1763. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  1764. spin_lock_init(&vm->status_lock);
  1765. INIT_LIST_HEAD(&vm->invalidated);
  1766. INIT_LIST_HEAD(&vm->cleared);
  1767. INIT_LIST_HEAD(&vm->freed);
  1768. /* create scheduler entity for page table updates */
  1769. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  1770. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  1771. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  1772. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  1773. r = amd_sched_entity_init(&ring->sched, &vm->entity,
  1774. rq, amdgpu_sched_jobs);
  1775. if (r)
  1776. return r;
  1777. vm->last_dir_update = NULL;
  1778. r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
  1779. AMDGPU_GEM_DOMAIN_VRAM,
  1780. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1781. AMDGPU_GEM_CREATE_SHADOW |
  1782. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  1783. AMDGPU_GEM_CREATE_VRAM_CLEARED,
  1784. NULL, NULL, &vm->root.bo);
  1785. if (r)
  1786. goto error_free_sched_entity;
  1787. r = amdgpu_bo_reserve(vm->root.bo, false);
  1788. if (r)
  1789. goto error_free_root;
  1790. vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
  1791. amdgpu_bo_unreserve(vm->root.bo);
  1792. return 0;
  1793. error_free_root:
  1794. amdgpu_bo_unref(&vm->root.bo->shadow);
  1795. amdgpu_bo_unref(&vm->root.bo);
  1796. vm->root.bo = NULL;
  1797. error_free_sched_entity:
  1798. amd_sched_entity_fini(&ring->sched, &vm->entity);
  1799. return r;
  1800. }
  1801. /**
  1802. * amdgpu_vm_free_levels - free PD/PT levels
  1803. *
  1804. * @level: PD/PT starting level to free
  1805. *
  1806. * Free the page directory or page table level and all sub levels.
  1807. */
  1808. static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
  1809. {
  1810. unsigned i;
  1811. if (level->bo) {
  1812. amdgpu_bo_unref(&level->bo->shadow);
  1813. amdgpu_bo_unref(&level->bo);
  1814. }
  1815. if (level->entries)
  1816. for (i = 0; i <= level->last_entry_used; i++)
  1817. amdgpu_vm_free_levels(&level->entries[i]);
  1818. drm_free_large(level->entries);
  1819. }
  1820. /**
  1821. * amdgpu_vm_fini - tear down a vm instance
  1822. *
  1823. * @adev: amdgpu_device pointer
  1824. * @vm: requested vm
  1825. *
  1826. * Tear down @vm.
  1827. * Unbind the VM and remove all bos from the vm bo list
  1828. */
  1829. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1830. {
  1831. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1832. bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
  1833. amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  1834. if (!RB_EMPTY_ROOT(&vm->va)) {
  1835. dev_err(adev->dev, "still active bo inside vm\n");
  1836. }
  1837. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1838. list_del(&mapping->list);
  1839. interval_tree_remove(&mapping->it, &vm->va);
  1840. kfree(mapping);
  1841. }
  1842. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1843. if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
  1844. amdgpu_vm_prt_fini(adev, vm);
  1845. prt_fini_needed = false;
  1846. }
  1847. list_del(&mapping->list);
  1848. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  1849. }
  1850. amdgpu_vm_free_levels(&vm->root);
  1851. dma_fence_put(vm->last_dir_update);
  1852. }
  1853. /**
  1854. * amdgpu_vm_manager_init - init the VM manager
  1855. *
  1856. * @adev: amdgpu_device pointer
  1857. *
  1858. * Initialize the VM manager structures
  1859. */
  1860. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  1861. {
  1862. unsigned i;
  1863. INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
  1864. /* skip over VMID 0, since it is the system VM */
  1865. for (i = 1; i < adev->vm_manager.num_ids; ++i) {
  1866. amdgpu_vm_reset_id(adev, i);
  1867. amdgpu_sync_create(&adev->vm_manager.ids[i].active);
  1868. list_add_tail(&adev->vm_manager.ids[i].list,
  1869. &adev->vm_manager.ids_lru);
  1870. }
  1871. adev->vm_manager.fence_context =
  1872. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1873. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1874. adev->vm_manager.seqno[i] = 0;
  1875. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  1876. atomic64_set(&adev->vm_manager.client_counter, 0);
  1877. spin_lock_init(&adev->vm_manager.prt_lock);
  1878. atomic_set(&adev->vm_manager.num_prt_users, 0);
  1879. }
  1880. /**
  1881. * amdgpu_vm_manager_fini - cleanup VM manager
  1882. *
  1883. * @adev: amdgpu_device pointer
  1884. *
  1885. * Cleanup the VM manager and free resources.
  1886. */
  1887. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  1888. {
  1889. unsigned i;
  1890. for (i = 0; i < AMDGPU_NUM_VM; ++i) {
  1891. struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
  1892. dma_fence_put(adev->vm_manager.ids[i].first);
  1893. amdgpu_sync_free(&adev->vm_manager.ids[i].active);
  1894. dma_fence_put(id->flushed_updates);
  1895. dma_fence_put(id->last_flush);
  1896. }
  1897. }