radeon_vm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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 = kmalloc_array(vm->max_pde_used + 2,
  125. sizeof(struct radeon_cs_reloc), GFP_KERNEL);
  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].tiling_flags = 0;
  135. list[0].handle = 0;
  136. list_add(&list[0].tv.head, head);
  137. for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
  138. if (!vm->page_tables[i].bo)
  139. continue;
  140. list[idx].gobj = NULL;
  141. list[idx].robj = vm->page_tables[i].bo;
  142. list[idx].prefered_domains = RADEON_GEM_DOMAIN_VRAM;
  143. list[idx].allowed_domains = RADEON_GEM_DOMAIN_VRAM;
  144. list[idx].tv.bo = &list[idx].robj->tbo;
  145. list[idx].tiling_flags = 0;
  146. list[idx].handle = 0;
  147. list_add(&list[idx++].tv.head, head);
  148. }
  149. return list;
  150. }
  151. /**
  152. * radeon_vm_grab_id - allocate the next free VMID
  153. *
  154. * @rdev: radeon_device pointer
  155. * @vm: vm to allocate id for
  156. * @ring: ring we want to submit job to
  157. *
  158. * Allocate an id for the vm (cayman+).
  159. * Returns the fence we need to sync to (if any).
  160. *
  161. * Global and local mutex must be locked!
  162. */
  163. struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
  164. struct radeon_vm *vm, int ring)
  165. {
  166. struct radeon_fence *best[RADEON_NUM_RINGS] = {};
  167. unsigned choices[2] = {};
  168. unsigned i;
  169. /* check if the id is still valid */
  170. if (vm->last_id_use && vm->last_id_use == rdev->vm_manager.active[vm->id])
  171. return NULL;
  172. /* we definately need to flush */
  173. radeon_fence_unref(&vm->last_flush);
  174. /* skip over VMID 0, since it is the system VM */
  175. for (i = 1; i < rdev->vm_manager.nvm; ++i) {
  176. struct radeon_fence *fence = rdev->vm_manager.active[i];
  177. if (fence == NULL) {
  178. /* found a free one */
  179. vm->id = i;
  180. trace_radeon_vm_grab_id(vm->id, ring);
  181. return NULL;
  182. }
  183. if (radeon_fence_is_earlier(fence, best[fence->ring])) {
  184. best[fence->ring] = fence;
  185. choices[fence->ring == ring ? 0 : 1] = i;
  186. }
  187. }
  188. for (i = 0; i < 2; ++i) {
  189. if (choices[i]) {
  190. vm->id = choices[i];
  191. trace_radeon_vm_grab_id(vm->id, ring);
  192. return rdev->vm_manager.active[choices[i]];
  193. }
  194. }
  195. /* should never happen */
  196. BUG();
  197. return NULL;
  198. }
  199. /**
  200. * radeon_vm_flush - hardware flush the vm
  201. *
  202. * @rdev: radeon_device pointer
  203. * @vm: vm we want to flush
  204. * @ring: ring to use for flush
  205. *
  206. * Flush the vm (cayman+).
  207. *
  208. * Global and local mutex must be locked!
  209. */
  210. void radeon_vm_flush(struct radeon_device *rdev,
  211. struct radeon_vm *vm,
  212. int ring)
  213. {
  214. uint64_t pd_addr = radeon_bo_gpu_offset(vm->page_directory);
  215. /* if we can't remember our last VM flush then flush now! */
  216. /* XXX figure out why we have to flush all the time */
  217. if (!vm->last_flush || true || pd_addr != vm->pd_gpu_addr) {
  218. vm->pd_gpu_addr = pd_addr;
  219. radeon_ring_vm_flush(rdev, ring, vm);
  220. }
  221. }
  222. /**
  223. * radeon_vm_fence - remember fence for vm
  224. *
  225. * @rdev: radeon_device pointer
  226. * @vm: vm we want to fence
  227. * @fence: fence to remember
  228. *
  229. * Fence the vm (cayman+).
  230. * Set the fence used to protect page table and id.
  231. *
  232. * Global and local mutex must be locked!
  233. */
  234. void radeon_vm_fence(struct radeon_device *rdev,
  235. struct radeon_vm *vm,
  236. struct radeon_fence *fence)
  237. {
  238. radeon_fence_unref(&vm->fence);
  239. vm->fence = radeon_fence_ref(fence);
  240. radeon_fence_unref(&rdev->vm_manager.active[vm->id]);
  241. rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence);
  242. radeon_fence_unref(&vm->last_id_use);
  243. vm->last_id_use = radeon_fence_ref(fence);
  244. /* we just flushed the VM, remember that */
  245. if (!vm->last_flush)
  246. vm->last_flush = radeon_fence_ref(fence);
  247. }
  248. /**
  249. * radeon_vm_bo_find - find the bo_va for a specific vm & bo
  250. *
  251. * @vm: requested vm
  252. * @bo: requested buffer object
  253. *
  254. * Find @bo inside the requested vm (cayman+).
  255. * Search inside the @bos vm list for the requested vm
  256. * Returns the found bo_va or NULL if none is found
  257. *
  258. * Object has to be reserved!
  259. */
  260. struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
  261. struct radeon_bo *bo)
  262. {
  263. struct radeon_bo_va *bo_va;
  264. list_for_each_entry(bo_va, &bo->va, bo_list) {
  265. if (bo_va->vm == vm) {
  266. return bo_va;
  267. }
  268. }
  269. return NULL;
  270. }
  271. /**
  272. * radeon_vm_bo_add - add a bo to a specific vm
  273. *
  274. * @rdev: radeon_device pointer
  275. * @vm: requested vm
  276. * @bo: radeon buffer object
  277. *
  278. * Add @bo into the requested vm (cayman+).
  279. * Add @bo to the list of bos associated with the vm
  280. * Returns newly added bo_va or NULL for failure
  281. *
  282. * Object has to be reserved!
  283. */
  284. struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
  285. struct radeon_vm *vm,
  286. struct radeon_bo *bo)
  287. {
  288. struct radeon_bo_va *bo_va;
  289. bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
  290. if (bo_va == NULL) {
  291. return NULL;
  292. }
  293. bo_va->vm = vm;
  294. bo_va->bo = bo;
  295. bo_va->soffset = 0;
  296. bo_va->eoffset = 0;
  297. bo_va->flags = 0;
  298. bo_va->valid = false;
  299. bo_va->ref_count = 1;
  300. INIT_LIST_HEAD(&bo_va->bo_list);
  301. INIT_LIST_HEAD(&bo_va->vm_list);
  302. INIT_LIST_HEAD(&bo_va->vm_status);
  303. mutex_lock(&vm->mutex);
  304. list_add(&bo_va->vm_list, &vm->va);
  305. list_add_tail(&bo_va->bo_list, &bo->va);
  306. mutex_unlock(&vm->mutex);
  307. return bo_va;
  308. }
  309. /**
  310. * radeon_vm_clear_bo - initially clear the page dir/table
  311. *
  312. * @rdev: radeon_device pointer
  313. * @bo: bo to clear
  314. */
  315. static int radeon_vm_clear_bo(struct radeon_device *rdev,
  316. struct radeon_bo *bo)
  317. {
  318. struct ttm_validate_buffer tv;
  319. struct ww_acquire_ctx ticket;
  320. struct list_head head;
  321. struct radeon_ib ib;
  322. unsigned entries;
  323. uint64_t addr;
  324. int r;
  325. memset(&tv, 0, sizeof(tv));
  326. tv.bo = &bo->tbo;
  327. INIT_LIST_HEAD(&head);
  328. list_add(&tv.head, &head);
  329. r = ttm_eu_reserve_buffers(&ticket, &head);
  330. if (r)
  331. return r;
  332. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  333. if (r)
  334. goto error;
  335. addr = radeon_bo_gpu_offset(bo);
  336. entries = radeon_bo_size(bo) / 8;
  337. r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib,
  338. NULL, entries * 2 + 64);
  339. if (r)
  340. goto error;
  341. ib.length_dw = 0;
  342. radeon_asic_vm_set_page(rdev, &ib, addr, 0, entries, 0, 0);
  343. r = radeon_ib_schedule(rdev, &ib, NULL);
  344. if (r)
  345. goto error;
  346. ttm_eu_fence_buffer_objects(&ticket, &head, ib.fence);
  347. radeon_ib_free(rdev, &ib);
  348. return 0;
  349. error:
  350. ttm_eu_backoff_reservation(&ticket, &head);
  351. return r;
  352. }
  353. /**
  354. * radeon_vm_bo_set_addr - set bos virtual address inside a vm
  355. *
  356. * @rdev: radeon_device pointer
  357. * @bo_va: bo_va to store the address
  358. * @soffset: requested offset of the buffer in the VM address space
  359. * @flags: attributes of pages (read/write/valid/etc.)
  360. *
  361. * Set offset of @bo_va (cayman+).
  362. * Validate and set the offset requested within the vm address space.
  363. * Returns 0 for success, error for failure.
  364. *
  365. * Object has to be reserved!
  366. */
  367. int radeon_vm_bo_set_addr(struct radeon_device *rdev,
  368. struct radeon_bo_va *bo_va,
  369. uint64_t soffset,
  370. uint32_t flags)
  371. {
  372. uint64_t size = radeon_bo_size(bo_va->bo);
  373. uint64_t eoffset, last_offset = 0;
  374. struct radeon_vm *vm = bo_va->vm;
  375. struct radeon_bo_va *tmp;
  376. struct list_head *head;
  377. unsigned last_pfn, pt_idx;
  378. int r;
  379. if (soffset) {
  380. /* make sure object fit at this offset */
  381. eoffset = soffset + size;
  382. if (soffset >= eoffset) {
  383. return -EINVAL;
  384. }
  385. last_pfn = eoffset / RADEON_GPU_PAGE_SIZE;
  386. if (last_pfn > rdev->vm_manager.max_pfn) {
  387. dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n",
  388. last_pfn, rdev->vm_manager.max_pfn);
  389. return -EINVAL;
  390. }
  391. } else {
  392. eoffset = last_pfn = 0;
  393. }
  394. mutex_lock(&vm->mutex);
  395. head = &vm->va;
  396. last_offset = 0;
  397. list_for_each_entry(tmp, &vm->va, vm_list) {
  398. if (bo_va == tmp) {
  399. /* skip over currently modified bo */
  400. continue;
  401. }
  402. if (soffset >= last_offset && eoffset <= tmp->soffset) {
  403. /* bo can be added before this one */
  404. break;
  405. }
  406. if (eoffset > tmp->soffset && soffset < tmp->eoffset) {
  407. /* bo and tmp overlap, invalid offset */
  408. dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n",
  409. bo_va->bo, (unsigned)bo_va->soffset, tmp->bo,
  410. (unsigned)tmp->soffset, (unsigned)tmp->eoffset);
  411. mutex_unlock(&vm->mutex);
  412. return -EINVAL;
  413. }
  414. last_offset = tmp->eoffset;
  415. head = &tmp->vm_list;
  416. }
  417. if (bo_va->soffset) {
  418. /* add a clone of the bo_va to clear the old address */
  419. tmp = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
  420. if (!tmp) {
  421. mutex_unlock(&vm->mutex);
  422. return -ENOMEM;
  423. }
  424. tmp->soffset = bo_va->soffset;
  425. tmp->eoffset = bo_va->eoffset;
  426. tmp->vm = vm;
  427. list_add(&tmp->vm_status, &vm->freed);
  428. }
  429. bo_va->soffset = soffset;
  430. bo_va->eoffset = eoffset;
  431. bo_va->flags = flags;
  432. bo_va->valid = false;
  433. list_move(&bo_va->vm_list, head);
  434. soffset = (soffset / RADEON_GPU_PAGE_SIZE) >> radeon_vm_block_size;
  435. eoffset = (eoffset / RADEON_GPU_PAGE_SIZE) >> radeon_vm_block_size;
  436. BUG_ON(eoffset >= radeon_vm_num_pdes(rdev));
  437. if (eoffset > vm->max_pde_used)
  438. vm->max_pde_used = eoffset;
  439. radeon_bo_unreserve(bo_va->bo);
  440. /* walk over the address space and allocate the page tables */
  441. for (pt_idx = soffset; pt_idx <= eoffset; ++pt_idx) {
  442. struct radeon_bo *pt;
  443. if (vm->page_tables[pt_idx].bo)
  444. continue;
  445. /* drop mutex to allocate and clear page table */
  446. mutex_unlock(&vm->mutex);
  447. r = radeon_bo_create(rdev, RADEON_VM_PTE_COUNT * 8,
  448. RADEON_GPU_PAGE_SIZE, true,
  449. RADEON_GEM_DOMAIN_VRAM, NULL, &pt);
  450. if (r)
  451. return r;
  452. r = radeon_vm_clear_bo(rdev, pt);
  453. if (r) {
  454. radeon_bo_unref(&pt);
  455. radeon_bo_reserve(bo_va->bo, false);
  456. return r;
  457. }
  458. /* aquire mutex again */
  459. mutex_lock(&vm->mutex);
  460. if (vm->page_tables[pt_idx].bo) {
  461. /* someone else allocated the pt in the meantime */
  462. mutex_unlock(&vm->mutex);
  463. radeon_bo_unref(&pt);
  464. mutex_lock(&vm->mutex);
  465. continue;
  466. }
  467. vm->page_tables[pt_idx].addr = 0;
  468. vm->page_tables[pt_idx].bo = pt;
  469. }
  470. mutex_unlock(&vm->mutex);
  471. return radeon_bo_reserve(bo_va->bo, false);
  472. }
  473. /**
  474. * radeon_vm_map_gart - get the physical address of a gart page
  475. *
  476. * @rdev: radeon_device pointer
  477. * @addr: the unmapped addr
  478. *
  479. * Look up the physical address of the page that the pte resolves
  480. * to (cayman+).
  481. * Returns the physical address of the page.
  482. */
  483. uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr)
  484. {
  485. uint64_t result;
  486. /* page table offset */
  487. result = rdev->gart.pages_addr[addr >> PAGE_SHIFT];
  488. /* in case cpu page size != gpu page size*/
  489. result |= addr & (~PAGE_MASK);
  490. return result;
  491. }
  492. /**
  493. * radeon_vm_page_flags - translate page flags to what the hw uses
  494. *
  495. * @flags: flags comming from userspace
  496. *
  497. * Translate the flags the userspace ABI uses to hw flags.
  498. */
  499. static uint32_t radeon_vm_page_flags(uint32_t flags)
  500. {
  501. uint32_t hw_flags = 0;
  502. hw_flags |= (flags & RADEON_VM_PAGE_VALID) ? R600_PTE_VALID : 0;
  503. hw_flags |= (flags & RADEON_VM_PAGE_READABLE) ? R600_PTE_READABLE : 0;
  504. hw_flags |= (flags & RADEON_VM_PAGE_WRITEABLE) ? R600_PTE_WRITEABLE : 0;
  505. if (flags & RADEON_VM_PAGE_SYSTEM) {
  506. hw_flags |= R600_PTE_SYSTEM;
  507. hw_flags |= (flags & RADEON_VM_PAGE_SNOOPED) ? R600_PTE_SNOOPED : 0;
  508. }
  509. return hw_flags;
  510. }
  511. /**
  512. * radeon_vm_update_pdes - make sure that page directory is valid
  513. *
  514. * @rdev: radeon_device pointer
  515. * @vm: requested vm
  516. * @start: start of GPU address range
  517. * @end: end of GPU address range
  518. *
  519. * Allocates new page tables if necessary
  520. * and updates the page directory (cayman+).
  521. * Returns 0 for success, error for failure.
  522. *
  523. * Global and local mutex must be locked!
  524. */
  525. int radeon_vm_update_page_directory(struct radeon_device *rdev,
  526. struct radeon_vm *vm)
  527. {
  528. struct radeon_bo *pd = vm->page_directory;
  529. uint64_t pd_addr = radeon_bo_gpu_offset(pd);
  530. uint32_t incr = RADEON_VM_PTE_COUNT * 8;
  531. uint64_t last_pde = ~0, last_pt = ~0;
  532. unsigned count = 0, pt_idx, ndw;
  533. struct radeon_ib ib;
  534. int r;
  535. /* padding, etc. */
  536. ndw = 64;
  537. /* assume the worst case */
  538. ndw += vm->max_pde_used * 16;
  539. /* update too big for an IB */
  540. if (ndw > 0xfffff)
  541. return -ENOMEM;
  542. r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, ndw * 4);
  543. if (r)
  544. return r;
  545. ib.length_dw = 0;
  546. /* walk over the address space and update the page directory */
  547. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  548. struct radeon_bo *bo = vm->page_tables[pt_idx].bo;
  549. uint64_t pde, pt;
  550. if (bo == NULL)
  551. continue;
  552. pt = radeon_bo_gpu_offset(bo);
  553. if (vm->page_tables[pt_idx].addr == pt)
  554. continue;
  555. vm->page_tables[pt_idx].addr = pt;
  556. pde = pd_addr + pt_idx * 8;
  557. if (((last_pde + 8 * count) != pde) ||
  558. ((last_pt + incr * count) != pt)) {
  559. if (count) {
  560. radeon_asic_vm_set_page(rdev, &ib, last_pde,
  561. last_pt, count, incr,
  562. R600_PTE_VALID);
  563. }
  564. count = 1;
  565. last_pde = pde;
  566. last_pt = pt;
  567. } else {
  568. ++count;
  569. }
  570. }
  571. if (count)
  572. radeon_asic_vm_set_page(rdev, &ib, last_pde, last_pt, count,
  573. incr, R600_PTE_VALID);
  574. if (ib.length_dw != 0) {
  575. radeon_semaphore_sync_to(ib.semaphore, pd->tbo.sync_obj);
  576. radeon_semaphore_sync_to(ib.semaphore, vm->last_id_use);
  577. r = radeon_ib_schedule(rdev, &ib, NULL);
  578. if (r) {
  579. radeon_ib_free(rdev, &ib);
  580. return r;
  581. }
  582. radeon_fence_unref(&vm->fence);
  583. vm->fence = radeon_fence_ref(ib.fence);
  584. radeon_fence_unref(&vm->last_flush);
  585. }
  586. radeon_ib_free(rdev, &ib);
  587. return 0;
  588. }
  589. /**
  590. * radeon_vm_frag_ptes - add fragment information to PTEs
  591. *
  592. * @rdev: radeon_device pointer
  593. * @ib: IB for the update
  594. * @pe_start: first PTE to handle
  595. * @pe_end: last PTE to handle
  596. * @addr: addr those PTEs should point to
  597. * @flags: hw mapping flags
  598. *
  599. * Global and local mutex must be locked!
  600. */
  601. static void radeon_vm_frag_ptes(struct radeon_device *rdev,
  602. struct radeon_ib *ib,
  603. uint64_t pe_start, uint64_t pe_end,
  604. uint64_t addr, uint32_t flags)
  605. {
  606. /**
  607. * The MC L1 TLB supports variable sized pages, based on a fragment
  608. * field in the PTE. When this field is set to a non-zero value, page
  609. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  610. * flags are considered valid for all PTEs within the fragment range
  611. * and corresponding mappings are assumed to be physically contiguous.
  612. *
  613. * The L1 TLB can store a single PTE for the whole fragment,
  614. * significantly increasing the space available for translation
  615. * caching. This leads to large improvements in throughput when the
  616. * TLB is under pressure.
  617. *
  618. * The L2 TLB distributes small and large fragments into two
  619. * asymmetric partitions. The large fragment cache is significantly
  620. * larger. Thus, we try to use large fragments wherever possible.
  621. * Userspace can support this by aligning virtual base address and
  622. * allocation size to the fragment size.
  623. */
  624. /* NI is optimized for 256KB fragments, SI and newer for 64KB */
  625. uint64_t frag_flags = rdev->family == CHIP_CAYMAN ?
  626. R600_PTE_FRAG_256KB : R600_PTE_FRAG_64KB;
  627. uint64_t frag_align = rdev->family == CHIP_CAYMAN ? 0x200 : 0x80;
  628. uint64_t frag_start = ALIGN(pe_start, frag_align);
  629. uint64_t frag_end = pe_end & ~(frag_align - 1);
  630. unsigned count;
  631. /* system pages are non continuously */
  632. if ((flags & R600_PTE_SYSTEM) || !(flags & R600_PTE_VALID) ||
  633. (frag_start >= frag_end)) {
  634. count = (pe_end - pe_start) / 8;
  635. radeon_asic_vm_set_page(rdev, ib, pe_start, addr, count,
  636. RADEON_GPU_PAGE_SIZE, flags);
  637. return;
  638. }
  639. /* handle the 4K area at the beginning */
  640. if (pe_start != frag_start) {
  641. count = (frag_start - pe_start) / 8;
  642. radeon_asic_vm_set_page(rdev, ib, pe_start, addr, count,
  643. RADEON_GPU_PAGE_SIZE, flags);
  644. addr += RADEON_GPU_PAGE_SIZE * count;
  645. }
  646. /* handle the area in the middle */
  647. count = (frag_end - frag_start) / 8;
  648. radeon_asic_vm_set_page(rdev, ib, frag_start, addr, count,
  649. RADEON_GPU_PAGE_SIZE, flags | frag_flags);
  650. /* handle the 4K area at the end */
  651. if (frag_end != pe_end) {
  652. addr += RADEON_GPU_PAGE_SIZE * count;
  653. count = (pe_end - frag_end) / 8;
  654. radeon_asic_vm_set_page(rdev, ib, frag_end, addr, count,
  655. RADEON_GPU_PAGE_SIZE, flags);
  656. }
  657. }
  658. /**
  659. * radeon_vm_update_ptes - make sure that page tables are valid
  660. *
  661. * @rdev: radeon_device pointer
  662. * @vm: requested vm
  663. * @start: start of GPU address range
  664. * @end: end of GPU address range
  665. * @dst: destination address to map to
  666. * @flags: mapping flags
  667. *
  668. * Update the page tables in the range @start - @end (cayman+).
  669. *
  670. * Global and local mutex must be locked!
  671. */
  672. static void radeon_vm_update_ptes(struct radeon_device *rdev,
  673. struct radeon_vm *vm,
  674. struct radeon_ib *ib,
  675. uint64_t start, uint64_t end,
  676. uint64_t dst, uint32_t flags)
  677. {
  678. uint64_t mask = RADEON_VM_PTE_COUNT - 1;
  679. uint64_t last_pte = ~0, last_dst = ~0;
  680. unsigned count = 0;
  681. uint64_t addr;
  682. start = start / RADEON_GPU_PAGE_SIZE;
  683. end = end / RADEON_GPU_PAGE_SIZE;
  684. /* walk over the address space and update the page tables */
  685. for (addr = start; addr < end; ) {
  686. uint64_t pt_idx = addr >> radeon_vm_block_size;
  687. struct radeon_bo *pt = vm->page_tables[pt_idx].bo;
  688. unsigned nptes;
  689. uint64_t pte;
  690. radeon_semaphore_sync_to(ib->semaphore, pt->tbo.sync_obj);
  691. if ((addr & ~mask) == (end & ~mask))
  692. nptes = end - addr;
  693. else
  694. nptes = RADEON_VM_PTE_COUNT - (addr & mask);
  695. pte = radeon_bo_gpu_offset(pt);
  696. pte += (addr & mask) * 8;
  697. if ((last_pte + 8 * count) != pte) {
  698. if (count) {
  699. radeon_vm_frag_ptes(rdev, ib, last_pte,
  700. last_pte + 8 * count,
  701. last_dst, flags);
  702. }
  703. count = nptes;
  704. last_pte = pte;
  705. last_dst = dst;
  706. } else {
  707. count += nptes;
  708. }
  709. addr += nptes;
  710. dst += nptes * RADEON_GPU_PAGE_SIZE;
  711. }
  712. if (count) {
  713. radeon_vm_frag_ptes(rdev, ib, last_pte,
  714. last_pte + 8 * count,
  715. last_dst, flags);
  716. }
  717. }
  718. /**
  719. * radeon_vm_bo_update - map a bo into the vm page table
  720. *
  721. * @rdev: radeon_device pointer
  722. * @vm: requested vm
  723. * @bo: radeon buffer object
  724. * @mem: ttm mem
  725. *
  726. * Fill in the page table entries for @bo (cayman+).
  727. * Returns 0 for success, -EINVAL for failure.
  728. *
  729. * Object have to be reserved and mutex must be locked!
  730. */
  731. int radeon_vm_bo_update(struct radeon_device *rdev,
  732. struct radeon_bo_va *bo_va,
  733. struct ttm_mem_reg *mem)
  734. {
  735. struct radeon_vm *vm = bo_va->vm;
  736. struct radeon_ib ib;
  737. unsigned nptes, ndw;
  738. uint64_t addr;
  739. int r;
  740. if (!bo_va->soffset) {
  741. dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n",
  742. bo_va->bo, vm);
  743. return -EINVAL;
  744. }
  745. if ((bo_va->valid && mem) || (!bo_va->valid && mem == NULL))
  746. return 0;
  747. bo_va->flags &= ~RADEON_VM_PAGE_VALID;
  748. bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
  749. if (mem) {
  750. addr = mem->start << PAGE_SHIFT;
  751. if (mem->mem_type != TTM_PL_SYSTEM) {
  752. bo_va->flags |= RADEON_VM_PAGE_VALID;
  753. bo_va->valid = true;
  754. }
  755. if (mem->mem_type == TTM_PL_TT) {
  756. bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
  757. } else {
  758. addr += rdev->vm_manager.vram_base_offset;
  759. }
  760. } else {
  761. addr = 0;
  762. bo_va->valid = false;
  763. }
  764. trace_radeon_vm_bo_update(bo_va);
  765. nptes = (bo_va->eoffset - bo_va->soffset) / RADEON_GPU_PAGE_SIZE;
  766. /* padding, etc. */
  767. ndw = 64;
  768. if (radeon_vm_block_size > 11)
  769. /* reserve space for one header for every 2k dwords */
  770. ndw += (nptes >> 11) * 4;
  771. else
  772. /* reserve space for one header for
  773. every (1 << BLOCK_SIZE) entries */
  774. ndw += (nptes >> radeon_vm_block_size) * 4;
  775. /* reserve space for pte addresses */
  776. ndw += nptes * 2;
  777. /* update too big for an IB */
  778. if (ndw > 0xfffff)
  779. return -ENOMEM;
  780. r = radeon_ib_get(rdev, R600_RING_TYPE_DMA_INDEX, &ib, NULL, ndw * 4);
  781. if (r)
  782. return r;
  783. ib.length_dw = 0;
  784. radeon_vm_update_ptes(rdev, vm, &ib, bo_va->soffset, bo_va->eoffset,
  785. addr, radeon_vm_page_flags(bo_va->flags));
  786. radeon_semaphore_sync_to(ib.semaphore, vm->fence);
  787. r = radeon_ib_schedule(rdev, &ib, NULL);
  788. if (r) {
  789. radeon_ib_free(rdev, &ib);
  790. return r;
  791. }
  792. radeon_fence_unref(&vm->fence);
  793. vm->fence = radeon_fence_ref(ib.fence);
  794. radeon_ib_free(rdev, &ib);
  795. radeon_fence_unref(&vm->last_flush);
  796. return 0;
  797. }
  798. /**
  799. * radeon_vm_clear_freed - clear freed BOs in the PT
  800. *
  801. * @rdev: radeon_device pointer
  802. * @vm: requested vm
  803. *
  804. * Make sure all freed BOs are cleared in the PT.
  805. * Returns 0 for success.
  806. *
  807. * PTs have to be reserved and mutex must be locked!
  808. */
  809. int radeon_vm_clear_freed(struct radeon_device *rdev,
  810. struct radeon_vm *vm)
  811. {
  812. struct radeon_bo_va *bo_va, *tmp;
  813. int r;
  814. list_for_each_entry_safe(bo_va, tmp, &vm->freed, vm_status) {
  815. list_del(&bo_va->vm_status);
  816. r = radeon_vm_bo_update(rdev, bo_va, NULL);
  817. kfree(bo_va);
  818. if (r)
  819. return r;
  820. }
  821. return 0;
  822. }
  823. /**
  824. * radeon_vm_bo_rmv - remove a bo to a specific vm
  825. *
  826. * @rdev: radeon_device pointer
  827. * @bo_va: requested bo_va
  828. *
  829. * Remove @bo_va->bo from the requested vm (cayman+).
  830. *
  831. * Object have to be reserved!
  832. */
  833. void radeon_vm_bo_rmv(struct radeon_device *rdev,
  834. struct radeon_bo_va *bo_va)
  835. {
  836. struct radeon_vm *vm = bo_va->vm;
  837. list_del(&bo_va->bo_list);
  838. mutex_lock(&vm->mutex);
  839. list_del(&bo_va->vm_list);
  840. if (bo_va->soffset) {
  841. bo_va->bo = NULL;
  842. list_add(&bo_va->vm_status, &vm->freed);
  843. } else {
  844. kfree(bo_va);
  845. }
  846. mutex_unlock(&vm->mutex);
  847. }
  848. /**
  849. * radeon_vm_bo_invalidate - mark the bo as invalid
  850. *
  851. * @rdev: radeon_device pointer
  852. * @vm: requested vm
  853. * @bo: radeon buffer object
  854. *
  855. * Mark @bo as invalid (cayman+).
  856. */
  857. void radeon_vm_bo_invalidate(struct radeon_device *rdev,
  858. struct radeon_bo *bo)
  859. {
  860. struct radeon_bo_va *bo_va;
  861. list_for_each_entry(bo_va, &bo->va, bo_list) {
  862. bo_va->valid = false;
  863. }
  864. }
  865. /**
  866. * radeon_vm_init - initialize a vm instance
  867. *
  868. * @rdev: radeon_device pointer
  869. * @vm: requested vm
  870. *
  871. * Init @vm fields (cayman+).
  872. */
  873. int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
  874. {
  875. const unsigned align = min(RADEON_VM_PTB_ALIGN_SIZE,
  876. RADEON_VM_PTE_COUNT * 8);
  877. unsigned pd_size, pd_entries, pts_size;
  878. int r;
  879. vm->id = 0;
  880. vm->ib_bo_va = NULL;
  881. vm->fence = NULL;
  882. vm->last_flush = NULL;
  883. vm->last_id_use = NULL;
  884. mutex_init(&vm->mutex);
  885. INIT_LIST_HEAD(&vm->va);
  886. INIT_LIST_HEAD(&vm->freed);
  887. pd_size = radeon_vm_directory_size(rdev);
  888. pd_entries = radeon_vm_num_pdes(rdev);
  889. /* allocate page table array */
  890. pts_size = pd_entries * sizeof(struct radeon_vm_pt);
  891. vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
  892. if (vm->page_tables == NULL) {
  893. DRM_ERROR("Cannot allocate memory for page table array\n");
  894. return -ENOMEM;
  895. }
  896. r = radeon_bo_create(rdev, pd_size, align, true,
  897. RADEON_GEM_DOMAIN_VRAM, NULL,
  898. &vm->page_directory);
  899. if (r)
  900. return r;
  901. r = radeon_vm_clear_bo(rdev, vm->page_directory);
  902. if (r) {
  903. radeon_bo_unref(&vm->page_directory);
  904. vm->page_directory = NULL;
  905. return r;
  906. }
  907. return 0;
  908. }
  909. /**
  910. * radeon_vm_fini - tear down a vm instance
  911. *
  912. * @rdev: radeon_device pointer
  913. * @vm: requested vm
  914. *
  915. * Tear down @vm (cayman+).
  916. * Unbind the VM and remove all bos from the vm bo list
  917. */
  918. void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
  919. {
  920. struct radeon_bo_va *bo_va, *tmp;
  921. int i, r;
  922. if (!list_empty(&vm->va)) {
  923. dev_err(rdev->dev, "still active bo inside vm\n");
  924. }
  925. list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) {
  926. list_del_init(&bo_va->vm_list);
  927. r = radeon_bo_reserve(bo_va->bo, false);
  928. if (!r) {
  929. list_del_init(&bo_va->bo_list);
  930. radeon_bo_unreserve(bo_va->bo);
  931. kfree(bo_va);
  932. }
  933. }
  934. list_for_each_entry_safe(bo_va, tmp, &vm->freed, vm_status)
  935. kfree(bo_va);
  936. for (i = 0; i < radeon_vm_num_pdes(rdev); i++)
  937. radeon_bo_unref(&vm->page_tables[i].bo);
  938. kfree(vm->page_tables);
  939. radeon_bo_unref(&vm->page_directory);
  940. radeon_fence_unref(&vm->fence);
  941. radeon_fence_unref(&vm->last_flush);
  942. radeon_fence_unref(&vm->last_id_use);
  943. mutex_destroy(&vm->mutex);
  944. }