radeon_vm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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 <drm/drmP.h>
  29. #include <drm/radeon_drm.h>
  30. #include "radeon.h"
  31. #include "radeon_trace.h"
  32. /*
  33. * GPUVM
  34. * GPUVM is similar to the legacy gart on older asics, however
  35. * rather than there being a single global gart table
  36. * for the entire GPU, there are multiple VM page tables active
  37. * at any given time. The VM page tables can contain a mix
  38. * vram pages and system memory pages and system memory pages
  39. * can be mapped as snooped (cached system pages) or unsnooped
  40. * (uncached system pages).
  41. * Each VM has an ID associated with it and there is a page table
  42. * associated with each VMID. When execting a command buffer,
  43. * the kernel tells the the ring what VMID to use for that command
  44. * buffer. VMIDs are allocated dynamically as commands are submitted.
  45. * The userspace drivers maintain their own address space and the kernel
  46. * sets up their pages tables accordingly when they submit their
  47. * command buffers and a VMID is assigned.
  48. * Cayman/Trinity support up to 8 active VMs at any given time;
  49. * SI supports 16.
  50. */
  51. /**
  52. * radeon_vm_num_pde - return the number of page directory entries
  53. *
  54. * @rdev: radeon_device pointer
  55. *
  56. * Calculate the number of page directory entries (cayman+).
  57. */
  58. static unsigned radeon_vm_num_pdes(struct radeon_device *rdev)
  59. {
  60. return rdev->vm_manager.max_pfn >> radeon_vm_block_size;
  61. }
  62. /**
  63. * radeon_vm_directory_size - returns the size of the page directory in bytes
  64. *
  65. * @rdev: radeon_device pointer
  66. *
  67. * Calculate the size of the page directory in bytes (cayman+).
  68. */
  69. static unsigned radeon_vm_directory_size(struct radeon_device *rdev)
  70. {
  71. return RADEON_GPU_PAGE_ALIGN(radeon_vm_num_pdes(rdev) * 8);
  72. }
  73. /**
  74. * radeon_vm_manager_init - init the vm manager
  75. *
  76. * @rdev: radeon_device pointer
  77. *
  78. * Init the vm manager (cayman+).
  79. * Returns 0 for success, error for failure.
  80. */
  81. int radeon_vm_manager_init(struct radeon_device *rdev)
  82. {
  83. int r;
  84. if (!rdev->vm_manager.enabled) {
  85. r = radeon_asic_vm_init(rdev);
  86. if (r)
  87. return r;
  88. rdev->vm_manager.enabled = true;
  89. }
  90. return 0;
  91. }
  92. /**
  93. * radeon_vm_manager_fini - tear down the vm manager
  94. *
  95. * @rdev: radeon_device pointer
  96. *
  97. * Tear down the VM manager (cayman+).
  98. */
  99. void radeon_vm_manager_fini(struct radeon_device *rdev)
  100. {
  101. int i;
  102. if (!rdev->vm_manager.enabled)
  103. return;
  104. for (i = 0; i < RADEON_NUM_VM; ++i)
  105. radeon_fence_unref(&rdev->vm_manager.active[i]);
  106. radeon_asic_vm_fini(rdev);
  107. rdev->vm_manager.enabled = false;
  108. }
  109. /**
  110. * radeon_vm_get_bos - add the vm BOs to a validation list
  111. *
  112. * @vm: vm providing the BOs
  113. * @head: head of validation list
  114. *
  115. * Add the page directory to the list of BOs to
  116. * validate for command submission (cayman+).
  117. */
  118. struct radeon_cs_reloc *radeon_vm_get_bos(struct radeon_device *rdev,
  119. struct radeon_vm *vm,
  120. struct list_head *head)
  121. {
  122. struct radeon_cs_reloc *list;
  123. unsigned i, idx;
  124. list = drm_malloc_ab(vm->max_pde_used + 2,
  125. sizeof(struct radeon_cs_reloc));
  126. if (!list)
  127. return NULL;
  128. /* add the vm page table to the list */
  129. list[0].gobj = NULL;
  130. list[0].robj = vm->page_directory;
  131. list[0].prefered_domains = RADEON_GEM_DOMAIN_VRAM;
  132. list[0].allowed_domains = RADEON_GEM_DOMAIN_VRAM;
  133. list[0].tv.bo = &vm->page_directory->tbo;
  134. list[0].tv.shared = false;
  135. list[0].tiling_flags = 0;
  136. list[0].handle = 0;
  137. list_add(&list[0].tv.head, head);
  138. for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
  139. if (!vm->page_tables[i].bo)
  140. continue;
  141. list[idx].gobj = NULL;
  142. list[idx].robj = vm->page_tables[i].bo;
  143. list[idx].prefered_domains = RADEON_GEM_DOMAIN_VRAM;
  144. list[idx].allowed_domains = RADEON_GEM_DOMAIN_VRAM;
  145. list[idx].tv.bo = &list[idx].robj->tbo;
  146. list[idx].tv.shared = false;
  147. list[idx].tiling_flags = 0;
  148. list[idx].handle = 0;
  149. list_add(&list[idx++].tv.head, head);
  150. }
  151. return list;
  152. }
  153. /**
  154. * radeon_vm_grab_id - allocate the next free VMID
  155. *
  156. * @rdev: radeon_device pointer
  157. * @vm: vm to allocate id for
  158. * @ring: ring we want to submit job to
  159. *
  160. * Allocate an id for the vm (cayman+).
  161. * Returns the fence we need to sync to (if any).
  162. *
  163. * Global and local mutex must be locked!
  164. */
  165. struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
  166. struct radeon_vm *vm, int ring)
  167. {
  168. struct radeon_fence *best[RADEON_NUM_RINGS] = {};
  169. unsigned choices[2] = {};
  170. unsigned i;
  171. /* check if the id is still valid */
  172. if (vm->last_id_use && vm->last_id_use == rdev->vm_manager.active[vm->id])
  173. return NULL;
  174. /* we definately need to flush */
  175. radeon_fence_unref(&vm->last_flush);
  176. /* skip over VMID 0, since it is the system VM */
  177. for (i = 1; i < rdev->vm_manager.nvm; ++i) {
  178. struct radeon_fence *fence = rdev->vm_manager.active[i];
  179. if (fence == NULL) {
  180. /* found a free one */
  181. vm->id = i;
  182. trace_radeon_vm_grab_id(vm->id, ring);
  183. return NULL;
  184. }
  185. if (radeon_fence_is_earlier(fence, best[fence->ring])) {
  186. best[fence->ring] = fence;
  187. choices[fence->ring == ring ? 0 : 1] = i;
  188. }
  189. }
  190. for (i = 0; i < 2; ++i) {
  191. if (choices[i]) {
  192. vm->id = choices[i];
  193. trace_radeon_vm_grab_id(vm->id, ring);
  194. return rdev->vm_manager.active[choices[i]];
  195. }
  196. }
  197. /* should never happen */
  198. BUG();
  199. return NULL;
  200. }
  201. /**
  202. * radeon_vm_flush - hardware flush the vm
  203. *
  204. * @rdev: radeon_device pointer
  205. * @vm: vm we want to flush
  206. * @ring: ring to use for flush
  207. *
  208. * Flush the vm (cayman+).
  209. *
  210. * Global and local mutex must be locked!
  211. */
  212. void radeon_vm_flush(struct radeon_device *rdev,
  213. struct radeon_vm *vm,
  214. int ring)
  215. {
  216. uint64_t pd_addr = radeon_bo_gpu_offset(vm->page_directory);
  217. /* if we can't remember our last VM flush then flush now! */
  218. if (!vm->last_flush || pd_addr != vm->pd_gpu_addr) {
  219. trace_radeon_vm_flush(pd_addr, ring, vm->id);
  220. vm->pd_gpu_addr = pd_addr;
  221. radeon_ring_vm_flush(rdev, ring, vm);
  222. }
  223. }
  224. /**
  225. * radeon_vm_fence - remember fence for vm
  226. *
  227. * @rdev: radeon_device pointer
  228. * @vm: vm we want to fence
  229. * @fence: fence to remember
  230. *
  231. * Fence the vm (cayman+).
  232. * Set the fence used to protect page table and id.
  233. *
  234. * Global and local mutex must be locked!
  235. */
  236. void radeon_vm_fence(struct radeon_device *rdev,
  237. struct radeon_vm *vm,
  238. struct radeon_fence *fence)
  239. {
  240. radeon_fence_unref(&vm->fence);
  241. vm->fence = radeon_fence_ref(fence);
  242. radeon_fence_unref(&rdev->vm_manager.active[vm->id]);
  243. rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence);
  244. radeon_fence_unref(&vm->last_id_use);
  245. vm->last_id_use = radeon_fence_ref(fence);
  246. /* we just flushed the VM, remember that */
  247. if (!vm->last_flush)
  248. vm->last_flush = radeon_fence_ref(fence);
  249. }
  250. /**
  251. * radeon_vm_bo_find - find the bo_va for a specific vm & bo
  252. *
  253. * @vm: requested vm
  254. * @bo: requested buffer object
  255. *
  256. * Find @bo inside the requested vm (cayman+).
  257. * Search inside the @bos vm list for the requested vm
  258. * Returns the found bo_va or NULL if none is found
  259. *
  260. * Object has to be reserved!
  261. */
  262. struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
  263. struct radeon_bo *bo)
  264. {
  265. struct radeon_bo_va *bo_va;
  266. list_for_each_entry(bo_va, &bo->va, bo_list) {
  267. if (bo_va->vm == vm) {
  268. return bo_va;
  269. }
  270. }
  271. return NULL;
  272. }
  273. /**
  274. * radeon_vm_bo_add - add a bo to a specific vm
  275. *
  276. * @rdev: radeon_device pointer
  277. * @vm: requested vm
  278. * @bo: radeon buffer object
  279. *
  280. * Add @bo into the requested vm (cayman+).
  281. * Add @bo to the list of bos associated with the vm
  282. * Returns newly added bo_va or NULL for failure
  283. *
  284. * Object has to be reserved!
  285. */
  286. struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
  287. struct radeon_vm *vm,
  288. struct radeon_bo *bo)
  289. {
  290. struct radeon_bo_va *bo_va;
  291. bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
  292. if (bo_va == NULL) {
  293. return NULL;
  294. }
  295. bo_va->vm = vm;
  296. bo_va->bo = bo;
  297. bo_va->it.start = 0;
  298. bo_va->it.last = 0;
  299. bo_va->flags = 0;
  300. bo_va->addr = 0;
  301. bo_va->ref_count = 1;
  302. INIT_LIST_HEAD(&bo_va->bo_list);
  303. INIT_LIST_HEAD(&bo_va->vm_status);
  304. mutex_lock(&vm->mutex);
  305. list_add_tail(&bo_va->bo_list, &bo->va);
  306. mutex_unlock(&vm->mutex);
  307. return bo_va;
  308. }
  309. /**
  310. * radeon_vm_set_pages - helper to call the right asic function
  311. *
  312. * @rdev: radeon_device pointer
  313. * @ib: indirect buffer to fill with commands
  314. * @pe: addr of the page entry
  315. * @addr: dst addr to write into pe
  316. * @count: number of page entries to update
  317. * @incr: increase next addr by incr bytes
  318. * @flags: hw access flags
  319. *
  320. * Traces the parameters and calls the right asic functions
  321. * to setup the page table using the DMA.
  322. */
  323. static void radeon_vm_set_pages(struct radeon_device *rdev,
  324. struct radeon_ib *ib,
  325. uint64_t pe,
  326. uint64_t addr, unsigned count,
  327. uint32_t incr, uint32_t flags)
  328. {
  329. trace_radeon_vm_set_page(pe, addr, count, incr, flags);
  330. if ((flags & R600_PTE_GART_MASK) == R600_PTE_GART_MASK) {
  331. uint64_t src = rdev->gart.table_addr + (addr >> 12) * 8;
  332. radeon_asic_vm_copy_pages(rdev, ib, pe, src, count);
  333. } else if ((flags & R600_PTE_SYSTEM) || (count < 3)) {
  334. radeon_asic_vm_write_pages(rdev, ib, pe, addr,
  335. count, incr, flags);
  336. } else {
  337. radeon_asic_vm_set_pages(rdev, ib, pe, addr,
  338. count, incr, flags);
  339. }
  340. }
  341. /**
  342. * radeon_vm_clear_bo - initially clear the page dir/table
  343. *
  344. * @rdev: radeon_device pointer
  345. * @bo: bo to clear
  346. */
  347. static int radeon_vm_clear_bo(struct radeon_device *rdev,
  348. struct radeon_bo *bo)
  349. {
  350. struct ttm_validate_buffer tv;
  351. struct ww_acquire_ctx ticket;
  352. struct list_head head;
  353. struct radeon_ib ib;
  354. unsigned entries;
  355. uint64_t addr;
  356. int r;
  357. memset(&tv, 0, sizeof(tv));
  358. tv.bo = &bo->tbo;
  359. tv.shared = false;
  360. INIT_LIST_HEAD(&head);
  361. list_add(&tv.head, &head);
  362. r = ttm_eu_reserve_buffers(&ticket, &head, true);
  363. if (r)
  364. return r;
  365. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  366. if (r)
  367. goto error;
  368. addr = radeon_bo_gpu_offset(bo);
  369. entries = radeon_bo_size(bo) / 8;
  370. r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, 256);
  371. if (r)
  372. goto error;
  373. ib.length_dw = 0;
  374. radeon_vm_set_pages(rdev, &ib, addr, 0, entries, 0, 0);
  375. radeon_asic_vm_pad_ib(rdev, &ib);
  376. WARN_ON(ib.length_dw > 64);
  377. r = radeon_ib_schedule(rdev, &ib, NULL, false);
  378. if (r)
  379. goto error;
  380. ttm_eu_fence_buffer_objects(&ticket, &head, &ib.fence->base);
  381. radeon_ib_free(rdev, &ib);
  382. return 0;
  383. error:
  384. ttm_eu_backoff_reservation(&ticket, &head);
  385. return r;
  386. }
  387. /**
  388. * radeon_vm_bo_set_addr - set bos virtual address inside a vm
  389. *
  390. * @rdev: radeon_device pointer
  391. * @bo_va: bo_va to store the address
  392. * @soffset: requested offset of the buffer in the VM address space
  393. * @flags: attributes of pages (read/write/valid/etc.)
  394. *
  395. * Set offset of @bo_va (cayman+).
  396. * Validate and set the offset requested within the vm address space.
  397. * Returns 0 for success, error for failure.
  398. *
  399. * Object has to be reserved!
  400. */
  401. int radeon_vm_bo_set_addr(struct radeon_device *rdev,
  402. struct radeon_bo_va *bo_va,
  403. uint64_t soffset,
  404. uint32_t flags)
  405. {
  406. uint64_t size = radeon_bo_size(bo_va->bo);
  407. struct radeon_vm *vm = bo_va->vm;
  408. unsigned last_pfn, pt_idx;
  409. uint64_t eoffset;
  410. int r;
  411. if (soffset) {
  412. /* make sure object fit at this offset */
  413. eoffset = soffset + size;
  414. if (soffset >= eoffset) {
  415. return -EINVAL;
  416. }
  417. last_pfn = eoffset / RADEON_GPU_PAGE_SIZE;
  418. if (last_pfn > rdev->vm_manager.max_pfn) {
  419. dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n",
  420. last_pfn, rdev->vm_manager.max_pfn);
  421. return -EINVAL;
  422. }
  423. } else {
  424. eoffset = last_pfn = 0;
  425. }
  426. mutex_lock(&vm->mutex);
  427. soffset /= RADEON_GPU_PAGE_SIZE;
  428. eoffset /= RADEON_GPU_PAGE_SIZE;
  429. if (soffset || eoffset) {
  430. struct interval_tree_node *it;
  431. it = interval_tree_iter_first(&vm->va, soffset, eoffset - 1);
  432. if (it && it != &bo_va->it) {
  433. struct radeon_bo_va *tmp;
  434. tmp = container_of(it, struct radeon_bo_va, it);
  435. /* bo and tmp overlap, invalid offset */
  436. dev_err(rdev->dev, "bo %p va 0x%010Lx conflict with "
  437. "(bo %p 0x%010lx 0x%010lx)\n", bo_va->bo,
  438. soffset, tmp->bo, tmp->it.start, tmp->it.last);
  439. mutex_unlock(&vm->mutex);
  440. return -EINVAL;
  441. }
  442. }
  443. if (bo_va->it.start || bo_va->it.last) {
  444. if (bo_va->addr) {
  445. /* add a clone of the bo_va to clear the old address */
  446. struct radeon_bo_va *tmp;
  447. tmp = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
  448. if (!tmp) {
  449. mutex_unlock(&vm->mutex);
  450. return -ENOMEM;
  451. }
  452. tmp->it.start = bo_va->it.start;
  453. tmp->it.last = bo_va->it.last;
  454. tmp->vm = vm;
  455. tmp->addr = bo_va->addr;
  456. tmp->bo = radeon_bo_ref(bo_va->bo);
  457. list_add(&tmp->vm_status, &vm->freed);
  458. bo_va->addr = 0;
  459. }
  460. interval_tree_remove(&bo_va->it, &vm->va);
  461. bo_va->it.start = 0;
  462. bo_va->it.last = 0;
  463. }
  464. if (soffset || eoffset) {
  465. bo_va->it.start = soffset;
  466. bo_va->it.last = eoffset - 1;
  467. interval_tree_insert(&bo_va->it, &vm->va);
  468. }
  469. bo_va->flags = flags;
  470. bo_va->addr = 0;
  471. soffset >>= radeon_vm_block_size;
  472. eoffset >>= radeon_vm_block_size;
  473. BUG_ON(eoffset >= radeon_vm_num_pdes(rdev));
  474. if (eoffset > vm->max_pde_used)
  475. vm->max_pde_used = eoffset;
  476. radeon_bo_unreserve(bo_va->bo);
  477. /* walk over the address space and allocate the page tables */
  478. for (pt_idx = soffset; pt_idx <= eoffset; ++pt_idx) {
  479. struct radeon_bo *pt;
  480. if (vm->page_tables[pt_idx].bo)
  481. continue;
  482. /* drop mutex to allocate and clear page table */
  483. mutex_unlock(&vm->mutex);
  484. r = radeon_bo_create(rdev, RADEON_VM_PTE_COUNT * 8,
  485. RADEON_GPU_PAGE_SIZE, true,
  486. RADEON_GEM_DOMAIN_VRAM, 0,
  487. NULL, NULL, &pt);
  488. if (r)
  489. return r;
  490. r = radeon_vm_clear_bo(rdev, pt);
  491. if (r) {
  492. radeon_bo_unref(&pt);
  493. radeon_bo_reserve(bo_va->bo, false);
  494. return r;
  495. }
  496. /* aquire mutex again */
  497. mutex_lock(&vm->mutex);
  498. if (vm->page_tables[pt_idx].bo) {
  499. /* someone else allocated the pt in the meantime */
  500. mutex_unlock(&vm->mutex);
  501. radeon_bo_unref(&pt);
  502. mutex_lock(&vm->mutex);
  503. continue;
  504. }
  505. vm->page_tables[pt_idx].addr = 0;
  506. vm->page_tables[pt_idx].bo = pt;
  507. }
  508. mutex_unlock(&vm->mutex);
  509. return radeon_bo_reserve(bo_va->bo, false);
  510. }
  511. /**
  512. * radeon_vm_map_gart - get the physical address of a gart page
  513. *
  514. * @rdev: radeon_device pointer
  515. * @addr: the unmapped addr
  516. *
  517. * Look up the physical address of the page that the pte resolves
  518. * to (cayman+).
  519. * Returns the physical address of the page.
  520. */
  521. uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr)
  522. {
  523. uint64_t result;
  524. /* page table offset */
  525. result = rdev->gart.pages_addr[addr >> PAGE_SHIFT];
  526. /* in case cpu page size != gpu page size*/
  527. result |= addr & (~PAGE_MASK);
  528. return result;
  529. }
  530. /**
  531. * radeon_vm_page_flags - translate page flags to what the hw uses
  532. *
  533. * @flags: flags comming from userspace
  534. *
  535. * Translate the flags the userspace ABI uses to hw flags.
  536. */
  537. static uint32_t radeon_vm_page_flags(uint32_t flags)
  538. {
  539. uint32_t hw_flags = 0;
  540. hw_flags |= (flags & RADEON_VM_PAGE_VALID) ? R600_PTE_VALID : 0;
  541. hw_flags |= (flags & RADEON_VM_PAGE_READABLE) ? R600_PTE_READABLE : 0;
  542. hw_flags |= (flags & RADEON_VM_PAGE_WRITEABLE) ? R600_PTE_WRITEABLE : 0;
  543. if (flags & RADEON_VM_PAGE_SYSTEM) {
  544. hw_flags |= R600_PTE_SYSTEM;
  545. hw_flags |= (flags & RADEON_VM_PAGE_SNOOPED) ? R600_PTE_SNOOPED : 0;
  546. }
  547. return hw_flags;
  548. }
  549. /**
  550. * radeon_vm_update_pdes - make sure that page directory is valid
  551. *
  552. * @rdev: radeon_device pointer
  553. * @vm: requested vm
  554. * @start: start of GPU address range
  555. * @end: end of GPU address range
  556. *
  557. * Allocates new page tables if necessary
  558. * and updates the page directory (cayman+).
  559. * Returns 0 for success, error for failure.
  560. *
  561. * Global and local mutex must be locked!
  562. */
  563. int radeon_vm_update_page_directory(struct radeon_device *rdev,
  564. struct radeon_vm *vm)
  565. {
  566. struct radeon_bo *pd = vm->page_directory;
  567. uint64_t pd_addr = radeon_bo_gpu_offset(pd);
  568. uint32_t incr = RADEON_VM_PTE_COUNT * 8;
  569. uint64_t last_pde = ~0, last_pt = ~0;
  570. unsigned count = 0, pt_idx, ndw;
  571. struct radeon_ib ib;
  572. int r;
  573. /* padding, etc. */
  574. ndw = 64;
  575. /* assume the worst case */
  576. ndw += vm->max_pde_used * 6;
  577. /* update too big for an IB */
  578. if (ndw > 0xfffff)
  579. return -ENOMEM;
  580. r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, ndw * 4);
  581. if (r)
  582. return r;
  583. ib.length_dw = 0;
  584. /* walk over the address space and update the page directory */
  585. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  586. struct radeon_bo *bo = vm->page_tables[pt_idx].bo;
  587. uint64_t pde, pt;
  588. if (bo == NULL)
  589. continue;
  590. pt = radeon_bo_gpu_offset(bo);
  591. if (vm->page_tables[pt_idx].addr == pt)
  592. continue;
  593. vm->page_tables[pt_idx].addr = pt;
  594. pde = pd_addr + pt_idx * 8;
  595. if (((last_pde + 8 * count) != pde) ||
  596. ((last_pt + incr * count) != pt)) {
  597. if (count) {
  598. radeon_vm_set_pages(rdev, &ib, last_pde,
  599. last_pt, count, incr,
  600. R600_PTE_VALID);
  601. }
  602. count = 1;
  603. last_pde = pde;
  604. last_pt = pt;
  605. } else {
  606. ++count;
  607. }
  608. }
  609. if (count)
  610. radeon_vm_set_pages(rdev, &ib, last_pde, last_pt, count,
  611. incr, R600_PTE_VALID);
  612. if (ib.length_dw != 0) {
  613. radeon_asic_vm_pad_ib(rdev, &ib);
  614. radeon_semaphore_sync_resv(rdev, ib.semaphore, pd->tbo.resv, false);
  615. radeon_semaphore_sync_fence(ib.semaphore, vm->last_id_use);
  616. WARN_ON(ib.length_dw > ndw);
  617. r = radeon_ib_schedule(rdev, &ib, NULL, false);
  618. if (r) {
  619. radeon_ib_free(rdev, &ib);
  620. return r;
  621. }
  622. radeon_fence_unref(&vm->fence);
  623. vm->fence = radeon_fence_ref(ib.fence);
  624. radeon_fence_unref(&vm->last_flush);
  625. }
  626. radeon_ib_free(rdev, &ib);
  627. return 0;
  628. }
  629. /**
  630. * radeon_vm_frag_ptes - add fragment information to PTEs
  631. *
  632. * @rdev: radeon_device pointer
  633. * @ib: IB for the update
  634. * @pe_start: first PTE to handle
  635. * @pe_end: last PTE to handle
  636. * @addr: addr those PTEs should point to
  637. * @flags: hw mapping flags
  638. *
  639. * Global and local mutex must be locked!
  640. */
  641. static void radeon_vm_frag_ptes(struct radeon_device *rdev,
  642. struct radeon_ib *ib,
  643. uint64_t pe_start, uint64_t pe_end,
  644. uint64_t addr, uint32_t flags)
  645. {
  646. /**
  647. * The MC L1 TLB supports variable sized pages, based on a fragment
  648. * field in the PTE. When this field is set to a non-zero value, page
  649. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  650. * flags are considered valid for all PTEs within the fragment range
  651. * and corresponding mappings are assumed to be physically contiguous.
  652. *
  653. * The L1 TLB can store a single PTE for the whole fragment,
  654. * significantly increasing the space available for translation
  655. * caching. This leads to large improvements in throughput when the
  656. * TLB is under pressure.
  657. *
  658. * The L2 TLB distributes small and large fragments into two
  659. * asymmetric partitions. The large fragment cache is significantly
  660. * larger. Thus, we try to use large fragments wherever possible.
  661. * Userspace can support this by aligning virtual base address and
  662. * allocation size to the fragment size.
  663. */
  664. /* NI is optimized for 256KB fragments, SI and newer for 64KB */
  665. uint64_t frag_flags = ((rdev->family == CHIP_CAYMAN) ||
  666. (rdev->family == CHIP_ARUBA)) ?
  667. R600_PTE_FRAG_256KB : R600_PTE_FRAG_64KB;
  668. uint64_t frag_align = ((rdev->family == CHIP_CAYMAN) ||
  669. (rdev->family == CHIP_ARUBA)) ? 0x200 : 0x80;
  670. uint64_t frag_start = ALIGN(pe_start, frag_align);
  671. uint64_t frag_end = pe_end & ~(frag_align - 1);
  672. unsigned count;
  673. /* system pages are non continuously */
  674. if ((flags & R600_PTE_SYSTEM) || !(flags & R600_PTE_VALID) ||
  675. (frag_start >= frag_end)) {
  676. count = (pe_end - pe_start) / 8;
  677. radeon_vm_set_pages(rdev, ib, pe_start, addr, count,
  678. RADEON_GPU_PAGE_SIZE, flags);
  679. return;
  680. }
  681. /* handle the 4K area at the beginning */
  682. if (pe_start != frag_start) {
  683. count = (frag_start - pe_start) / 8;
  684. radeon_vm_set_pages(rdev, ib, pe_start, addr, count,
  685. RADEON_GPU_PAGE_SIZE, flags);
  686. addr += RADEON_GPU_PAGE_SIZE * count;
  687. }
  688. /* handle the area in the middle */
  689. count = (frag_end - frag_start) / 8;
  690. radeon_vm_set_pages(rdev, ib, frag_start, addr, count,
  691. RADEON_GPU_PAGE_SIZE, flags | frag_flags);
  692. /* handle the 4K area at the end */
  693. if (frag_end != pe_end) {
  694. addr += RADEON_GPU_PAGE_SIZE * count;
  695. count = (pe_end - frag_end) / 8;
  696. radeon_vm_set_pages(rdev, ib, frag_end, addr, count,
  697. RADEON_GPU_PAGE_SIZE, flags);
  698. }
  699. }
  700. /**
  701. * radeon_vm_update_ptes - make sure that page tables are valid
  702. *
  703. * @rdev: radeon_device pointer
  704. * @vm: requested vm
  705. * @start: start of GPU address range
  706. * @end: end of GPU address range
  707. * @dst: destination address to map to
  708. * @flags: mapping flags
  709. *
  710. * Update the page tables in the range @start - @end (cayman+).
  711. *
  712. * Global and local mutex must be locked!
  713. */
  714. static void radeon_vm_update_ptes(struct radeon_device *rdev,
  715. struct radeon_vm *vm,
  716. struct radeon_ib *ib,
  717. uint64_t start, uint64_t end,
  718. uint64_t dst, uint32_t flags)
  719. {
  720. uint64_t mask = RADEON_VM_PTE_COUNT - 1;
  721. uint64_t last_pte = ~0, last_dst = ~0;
  722. unsigned count = 0;
  723. uint64_t addr;
  724. /* walk over the address space and update the page tables */
  725. for (addr = start; addr < end; ) {
  726. uint64_t pt_idx = addr >> radeon_vm_block_size;
  727. struct radeon_bo *pt = vm->page_tables[pt_idx].bo;
  728. unsigned nptes;
  729. uint64_t pte;
  730. radeon_semaphore_sync_resv(rdev, ib->semaphore, pt->tbo.resv, false);
  731. if ((addr & ~mask) == (end & ~mask))
  732. nptes = end - addr;
  733. else
  734. nptes = RADEON_VM_PTE_COUNT - (addr & mask);
  735. pte = radeon_bo_gpu_offset(pt);
  736. pte += (addr & mask) * 8;
  737. if ((last_pte + 8 * count) != pte) {
  738. if (count) {
  739. radeon_vm_frag_ptes(rdev, ib, last_pte,
  740. last_pte + 8 * count,
  741. last_dst, flags);
  742. }
  743. count = nptes;
  744. last_pte = pte;
  745. last_dst = dst;
  746. } else {
  747. count += nptes;
  748. }
  749. addr += nptes;
  750. dst += nptes * RADEON_GPU_PAGE_SIZE;
  751. }
  752. if (count) {
  753. radeon_vm_frag_ptes(rdev, ib, last_pte,
  754. last_pte + 8 * count,
  755. last_dst, flags);
  756. }
  757. }
  758. /**
  759. * radeon_vm_bo_update - map a bo into the vm page table
  760. *
  761. * @rdev: radeon_device pointer
  762. * @vm: requested vm
  763. * @bo: radeon buffer object
  764. * @mem: ttm mem
  765. *
  766. * Fill in the page table entries for @bo (cayman+).
  767. * Returns 0 for success, -EINVAL for failure.
  768. *
  769. * Object have to be reserved and mutex must be locked!
  770. */
  771. int radeon_vm_bo_update(struct radeon_device *rdev,
  772. struct radeon_bo_va *bo_va,
  773. struct ttm_mem_reg *mem)
  774. {
  775. struct radeon_vm *vm = bo_va->vm;
  776. struct radeon_ib ib;
  777. unsigned nptes, ncmds, ndw;
  778. uint64_t addr;
  779. uint32_t flags;
  780. int r;
  781. if (!bo_va->it.start) {
  782. dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n",
  783. bo_va->bo, vm);
  784. return -EINVAL;
  785. }
  786. list_del_init(&bo_va->vm_status);
  787. bo_va->flags &= ~RADEON_VM_PAGE_VALID;
  788. bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
  789. bo_va->flags &= ~RADEON_VM_PAGE_SNOOPED;
  790. if (bo_va->bo && radeon_ttm_tt_is_readonly(bo_va->bo->tbo.ttm))
  791. bo_va->flags &= ~RADEON_VM_PAGE_WRITEABLE;
  792. if (mem) {
  793. addr = mem->start << PAGE_SHIFT;
  794. if (mem->mem_type != TTM_PL_SYSTEM) {
  795. bo_va->flags |= RADEON_VM_PAGE_VALID;
  796. }
  797. if (mem->mem_type == TTM_PL_TT) {
  798. bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
  799. if (!(bo_va->bo->flags & (RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC)))
  800. bo_va->flags |= RADEON_VM_PAGE_SNOOPED;
  801. } else {
  802. addr += rdev->vm_manager.vram_base_offset;
  803. }
  804. } else {
  805. addr = 0;
  806. }
  807. if (addr == bo_va->addr)
  808. return 0;
  809. bo_va->addr = addr;
  810. trace_radeon_vm_bo_update(bo_va);
  811. nptes = bo_va->it.last - bo_va->it.start + 1;
  812. /* reserve space for one command every (1 << BLOCK_SIZE) entries
  813. or 2k dwords (whatever is smaller) */
  814. ncmds = (nptes >> min(radeon_vm_block_size, 11)) + 1;
  815. /* padding, etc. */
  816. ndw = 64;
  817. flags = radeon_vm_page_flags(bo_va->flags);
  818. if ((flags & R600_PTE_GART_MASK) == R600_PTE_GART_MASK) {
  819. /* only copy commands needed */
  820. ndw += ncmds * 7;
  821. } else if (flags & R600_PTE_SYSTEM) {
  822. /* header for write data commands */
  823. ndw += ncmds * 4;
  824. /* body of write data command */
  825. ndw += nptes * 2;
  826. } else {
  827. /* set page commands needed */
  828. ndw += ncmds * 10;
  829. /* two extra commands for begin/end of fragment */
  830. ndw += 2 * 10;
  831. }
  832. /* update too big for an IB */
  833. if (ndw > 0xfffff)
  834. return -ENOMEM;
  835. r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, ndw * 4);
  836. if (r)
  837. return r;
  838. ib.length_dw = 0;
  839. radeon_vm_update_ptes(rdev, vm, &ib, bo_va->it.start,
  840. bo_va->it.last + 1, addr,
  841. radeon_vm_page_flags(bo_va->flags));
  842. radeon_asic_vm_pad_ib(rdev, &ib);
  843. WARN_ON(ib.length_dw > ndw);
  844. radeon_semaphore_sync_fence(ib.semaphore, vm->fence);
  845. r = radeon_ib_schedule(rdev, &ib, NULL, false);
  846. if (r) {
  847. radeon_ib_free(rdev, &ib);
  848. return r;
  849. }
  850. radeon_fence_unref(&vm->fence);
  851. vm->fence = radeon_fence_ref(ib.fence);
  852. radeon_ib_free(rdev, &ib);
  853. radeon_fence_unref(&vm->last_flush);
  854. return 0;
  855. }
  856. /**
  857. * radeon_vm_clear_freed - clear freed BOs in the PT
  858. *
  859. * @rdev: radeon_device pointer
  860. * @vm: requested vm
  861. *
  862. * Make sure all freed BOs are cleared in the PT.
  863. * Returns 0 for success.
  864. *
  865. * PTs have to be reserved and mutex must be locked!
  866. */
  867. int radeon_vm_clear_freed(struct radeon_device *rdev,
  868. struct radeon_vm *vm)
  869. {
  870. struct radeon_bo_va *bo_va, *tmp;
  871. int r;
  872. list_for_each_entry_safe(bo_va, tmp, &vm->freed, vm_status) {
  873. r = radeon_vm_bo_update(rdev, bo_va, NULL);
  874. radeon_bo_unref(&bo_va->bo);
  875. kfree(bo_va);
  876. if (r)
  877. return r;
  878. }
  879. return 0;
  880. }
  881. /**
  882. * radeon_vm_clear_invalids - clear invalidated BOs in the PT
  883. *
  884. * @rdev: radeon_device pointer
  885. * @vm: requested vm
  886. *
  887. * Make sure all invalidated BOs are cleared in the PT.
  888. * Returns 0 for success.
  889. *
  890. * PTs have to be reserved and mutex must be locked!
  891. */
  892. int radeon_vm_clear_invalids(struct radeon_device *rdev,
  893. struct radeon_vm *vm)
  894. {
  895. struct radeon_bo_va *bo_va, *tmp;
  896. int r;
  897. list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, vm_status) {
  898. r = radeon_vm_bo_update(rdev, bo_va, NULL);
  899. if (r)
  900. return r;
  901. }
  902. return 0;
  903. }
  904. /**
  905. * radeon_vm_bo_rmv - remove a bo to a specific vm
  906. *
  907. * @rdev: radeon_device pointer
  908. * @bo_va: requested bo_va
  909. *
  910. * Remove @bo_va->bo from the requested vm (cayman+).
  911. *
  912. * Object have to be reserved!
  913. */
  914. void radeon_vm_bo_rmv(struct radeon_device *rdev,
  915. struct radeon_bo_va *bo_va)
  916. {
  917. struct radeon_vm *vm = bo_va->vm;
  918. list_del(&bo_va->bo_list);
  919. mutex_lock(&vm->mutex);
  920. if (bo_va->it.start || bo_va->it.last)
  921. interval_tree_remove(&bo_va->it, &vm->va);
  922. list_del(&bo_va->vm_status);
  923. if (bo_va->addr) {
  924. bo_va->bo = radeon_bo_ref(bo_va->bo);
  925. list_add(&bo_va->vm_status, &vm->freed);
  926. } else {
  927. kfree(bo_va);
  928. }
  929. mutex_unlock(&vm->mutex);
  930. }
  931. /**
  932. * radeon_vm_bo_invalidate - mark the bo as invalid
  933. *
  934. * @rdev: radeon_device pointer
  935. * @vm: requested vm
  936. * @bo: radeon buffer object
  937. *
  938. * Mark @bo as invalid (cayman+).
  939. */
  940. void radeon_vm_bo_invalidate(struct radeon_device *rdev,
  941. struct radeon_bo *bo)
  942. {
  943. struct radeon_bo_va *bo_va;
  944. list_for_each_entry(bo_va, &bo->va, bo_list) {
  945. if (bo_va->addr) {
  946. mutex_lock(&bo_va->vm->mutex);
  947. list_del(&bo_va->vm_status);
  948. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  949. mutex_unlock(&bo_va->vm->mutex);
  950. }
  951. }
  952. }
  953. /**
  954. * radeon_vm_init - initialize a vm instance
  955. *
  956. * @rdev: radeon_device pointer
  957. * @vm: requested vm
  958. *
  959. * Init @vm fields (cayman+).
  960. */
  961. int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
  962. {
  963. const unsigned align = min(RADEON_VM_PTB_ALIGN_SIZE,
  964. RADEON_VM_PTE_COUNT * 8);
  965. unsigned pd_size, pd_entries, pts_size;
  966. int r;
  967. vm->id = 0;
  968. vm->ib_bo_va = NULL;
  969. vm->fence = NULL;
  970. vm->last_flush = NULL;
  971. vm->last_id_use = NULL;
  972. mutex_init(&vm->mutex);
  973. vm->va = RB_ROOT;
  974. INIT_LIST_HEAD(&vm->invalidated);
  975. INIT_LIST_HEAD(&vm->freed);
  976. pd_size = radeon_vm_directory_size(rdev);
  977. pd_entries = radeon_vm_num_pdes(rdev);
  978. /* allocate page table array */
  979. pts_size = pd_entries * sizeof(struct radeon_vm_pt);
  980. vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
  981. if (vm->page_tables == NULL) {
  982. DRM_ERROR("Cannot allocate memory for page table array\n");
  983. return -ENOMEM;
  984. }
  985. r = radeon_bo_create(rdev, pd_size, align, true,
  986. RADEON_GEM_DOMAIN_VRAM, 0, NULL,
  987. NULL, &vm->page_directory);
  988. if (r)
  989. return r;
  990. r = radeon_vm_clear_bo(rdev, vm->page_directory);
  991. if (r) {
  992. radeon_bo_unref(&vm->page_directory);
  993. vm->page_directory = NULL;
  994. return r;
  995. }
  996. return 0;
  997. }
  998. /**
  999. * radeon_vm_fini - tear down a vm instance
  1000. *
  1001. * @rdev: radeon_device pointer
  1002. * @vm: requested vm
  1003. *
  1004. * Tear down @vm (cayman+).
  1005. * Unbind the VM and remove all bos from the vm bo list
  1006. */
  1007. void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
  1008. {
  1009. struct radeon_bo_va *bo_va, *tmp;
  1010. int i, r;
  1011. if (!RB_EMPTY_ROOT(&vm->va)) {
  1012. dev_err(rdev->dev, "still active bo inside vm\n");
  1013. }
  1014. rbtree_postorder_for_each_entry_safe(bo_va, tmp, &vm->va, it.rb) {
  1015. interval_tree_remove(&bo_va->it, &vm->va);
  1016. r = radeon_bo_reserve(bo_va->bo, false);
  1017. if (!r) {
  1018. list_del_init(&bo_va->bo_list);
  1019. radeon_bo_unreserve(bo_va->bo);
  1020. kfree(bo_va);
  1021. }
  1022. }
  1023. list_for_each_entry_safe(bo_va, tmp, &vm->freed, vm_status) {
  1024. radeon_bo_unref(&bo_va->bo);
  1025. kfree(bo_va);
  1026. }
  1027. for (i = 0; i < radeon_vm_num_pdes(rdev); i++)
  1028. radeon_bo_unref(&vm->page_tables[i].bo);
  1029. kfree(vm->page_tables);
  1030. radeon_bo_unref(&vm->page_directory);
  1031. radeon_fence_unref(&vm->fence);
  1032. radeon_fence_unref(&vm->last_flush);
  1033. radeon_fence_unref(&vm->last_id_use);
  1034. mutex_destroy(&vm->mutex);
  1035. }