radeon_gart.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 "drmP.h"
  29. #include "radeon_drm.h"
  30. #include "radeon.h"
  31. #include "radeon_reg.h"
  32. /*
  33. * GART
  34. * The GART (Graphics Aperture Remapping Table) is an aperture
  35. * in the GPU's address space. System pages can be mapped into
  36. * the aperture and look like contiguous pages from the GPU's
  37. * perspective. A page table maps the pages in the aperture
  38. * to the actual backing pages in system memory.
  39. *
  40. * Radeon GPUs support both an internal GART, as described above,
  41. * and AGP. AGP works similarly, but the GART table is configured
  42. * and maintained by the northbridge rather than the driver.
  43. * Radeon hw has a separate AGP aperture that is programmed to
  44. * point to the AGP aperture provided by the northbridge and the
  45. * requests are passed through to the northbridge aperture.
  46. * Both AGP and internal GART can be used at the same time, however
  47. * that is not currently supported by the driver.
  48. *
  49. * This file handles the common internal GART management.
  50. */
  51. /*
  52. * Common GART table functions.
  53. */
  54. /**
  55. * radeon_gart_table_ram_alloc - allocate system ram for gart page table
  56. *
  57. * @rdev: radeon_device pointer
  58. *
  59. * Allocate system memory for GART page table
  60. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  61. * gart table to be in system memory.
  62. * Returns 0 for success, -ENOMEM for failure.
  63. */
  64. int radeon_gart_table_ram_alloc(struct radeon_device *rdev)
  65. {
  66. void *ptr;
  67. ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size,
  68. &rdev->gart.table_addr);
  69. if (ptr == NULL) {
  70. return -ENOMEM;
  71. }
  72. #ifdef CONFIG_X86
  73. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  74. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  75. set_memory_uc((unsigned long)ptr,
  76. rdev->gart.table_size >> PAGE_SHIFT);
  77. }
  78. #endif
  79. rdev->gart.ptr = ptr;
  80. memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size);
  81. return 0;
  82. }
  83. /**
  84. * radeon_gart_table_ram_free - free system ram for gart page table
  85. *
  86. * @rdev: radeon_device pointer
  87. *
  88. * Free system memory for GART page table
  89. * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
  90. * gart table to be in system memory.
  91. */
  92. void radeon_gart_table_ram_free(struct radeon_device *rdev)
  93. {
  94. if (rdev->gart.ptr == NULL) {
  95. return;
  96. }
  97. #ifdef CONFIG_X86
  98. if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
  99. rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
  100. set_memory_wb((unsigned long)rdev->gart.ptr,
  101. rdev->gart.table_size >> PAGE_SHIFT);
  102. }
  103. #endif
  104. pci_free_consistent(rdev->pdev, rdev->gart.table_size,
  105. (void *)rdev->gart.ptr,
  106. rdev->gart.table_addr);
  107. rdev->gart.ptr = NULL;
  108. rdev->gart.table_addr = 0;
  109. }
  110. /**
  111. * radeon_gart_table_vram_alloc - allocate vram for gart page table
  112. *
  113. * @rdev: radeon_device pointer
  114. *
  115. * Allocate video memory for GART page table
  116. * (pcie r4xx, r5xx+). These asics require the
  117. * gart table to be in video memory.
  118. * Returns 0 for success, error for failure.
  119. */
  120. int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
  121. {
  122. int r;
  123. if (rdev->gart.robj == NULL) {
  124. r = radeon_bo_create(rdev, rdev->gart.table_size,
  125. PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
  126. NULL, &rdev->gart.robj);
  127. if (r) {
  128. return r;
  129. }
  130. }
  131. return 0;
  132. }
  133. /**
  134. * radeon_gart_table_vram_pin - pin gart page table in vram
  135. *
  136. * @rdev: radeon_device pointer
  137. *
  138. * Pin the GART page table in vram so it will not be moved
  139. * by the memory manager (pcie r4xx, r5xx+). These asics require the
  140. * gart table to be in video memory.
  141. * Returns 0 for success, error for failure.
  142. */
  143. int radeon_gart_table_vram_pin(struct radeon_device *rdev)
  144. {
  145. uint64_t gpu_addr;
  146. int r;
  147. r = radeon_bo_reserve(rdev->gart.robj, false);
  148. if (unlikely(r != 0))
  149. return r;
  150. r = radeon_bo_pin(rdev->gart.robj,
  151. RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  152. if (r) {
  153. radeon_bo_unreserve(rdev->gart.robj);
  154. return r;
  155. }
  156. r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
  157. if (r)
  158. radeon_bo_unpin(rdev->gart.robj);
  159. radeon_bo_unreserve(rdev->gart.robj);
  160. rdev->gart.table_addr = gpu_addr;
  161. return r;
  162. }
  163. /**
  164. * radeon_gart_table_vram_unpin - unpin gart page table in vram
  165. *
  166. * @rdev: radeon_device pointer
  167. *
  168. * Unpin the GART page table in vram (pcie r4xx, r5xx+).
  169. * These asics require the gart table to be in video memory.
  170. */
  171. void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
  172. {
  173. int r;
  174. if (rdev->gart.robj == NULL) {
  175. return;
  176. }
  177. r = radeon_bo_reserve(rdev->gart.robj, false);
  178. if (likely(r == 0)) {
  179. radeon_bo_kunmap(rdev->gart.robj);
  180. radeon_bo_unpin(rdev->gart.robj);
  181. radeon_bo_unreserve(rdev->gart.robj);
  182. rdev->gart.ptr = NULL;
  183. }
  184. }
  185. /**
  186. * radeon_gart_table_vram_free - free gart page table vram
  187. *
  188. * @rdev: radeon_device pointer
  189. *
  190. * Free the video memory used for the GART page table
  191. * (pcie r4xx, r5xx+). These asics require the gart table to
  192. * be in video memory.
  193. */
  194. void radeon_gart_table_vram_free(struct radeon_device *rdev)
  195. {
  196. if (rdev->gart.robj == NULL) {
  197. return;
  198. }
  199. radeon_gart_table_vram_unpin(rdev);
  200. radeon_bo_unref(&rdev->gart.robj);
  201. }
  202. /*
  203. * Common gart functions.
  204. */
  205. /**
  206. * radeon_gart_unbind - unbind pages from the gart page table
  207. *
  208. * @rdev: radeon_device pointer
  209. * @offset: offset into the GPU's gart aperture
  210. * @pages: number of pages to unbind
  211. *
  212. * Unbinds the requested pages from the gart page table and
  213. * replaces them with the dummy page (all asics).
  214. */
  215. void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
  216. int pages)
  217. {
  218. unsigned t;
  219. unsigned p;
  220. int i, j;
  221. u64 page_base;
  222. if (!rdev->gart.ready) {
  223. WARN(1, "trying to unbind memory from uninitialized GART !\n");
  224. return;
  225. }
  226. t = offset / RADEON_GPU_PAGE_SIZE;
  227. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  228. for (i = 0; i < pages; i++, p++) {
  229. if (rdev->gart.pages[p]) {
  230. rdev->gart.pages[p] = NULL;
  231. rdev->gart.pages_addr[p] = rdev->dummy_page.addr;
  232. page_base = rdev->gart.pages_addr[p];
  233. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  234. if (rdev->gart.ptr) {
  235. radeon_gart_set_page(rdev, t, page_base);
  236. }
  237. page_base += RADEON_GPU_PAGE_SIZE;
  238. }
  239. }
  240. }
  241. mb();
  242. radeon_gart_tlb_flush(rdev);
  243. }
  244. /**
  245. * radeon_gart_bind - bind pages into the gart page table
  246. *
  247. * @rdev: radeon_device pointer
  248. * @offset: offset into the GPU's gart aperture
  249. * @pages: number of pages to bind
  250. * @pagelist: pages to bind
  251. * @dma_addr: DMA addresses of pages
  252. *
  253. * Binds the requested pages to the gart page table
  254. * (all asics).
  255. * Returns 0 for success, -EINVAL for failure.
  256. */
  257. int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
  258. int pages, struct page **pagelist, dma_addr_t *dma_addr)
  259. {
  260. unsigned t;
  261. unsigned p;
  262. uint64_t page_base;
  263. int i, j;
  264. if (!rdev->gart.ready) {
  265. WARN(1, "trying to bind memory to uninitialized GART !\n");
  266. return -EINVAL;
  267. }
  268. t = offset / RADEON_GPU_PAGE_SIZE;
  269. p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
  270. for (i = 0; i < pages; i++, p++) {
  271. rdev->gart.pages_addr[p] = dma_addr[i];
  272. rdev->gart.pages[p] = pagelist[i];
  273. if (rdev->gart.ptr) {
  274. page_base = rdev->gart.pages_addr[p];
  275. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  276. radeon_gart_set_page(rdev, t, page_base);
  277. page_base += RADEON_GPU_PAGE_SIZE;
  278. }
  279. }
  280. }
  281. mb();
  282. radeon_gart_tlb_flush(rdev);
  283. return 0;
  284. }
  285. /**
  286. * radeon_gart_restore - bind all pages in the gart page table
  287. *
  288. * @rdev: radeon_device pointer
  289. *
  290. * Binds all pages in the gart page table (all asics).
  291. * Used to rebuild the gart table on device startup or resume.
  292. */
  293. void radeon_gart_restore(struct radeon_device *rdev)
  294. {
  295. int i, j, t;
  296. u64 page_base;
  297. if (!rdev->gart.ptr) {
  298. return;
  299. }
  300. for (i = 0, t = 0; i < rdev->gart.num_cpu_pages; i++) {
  301. page_base = rdev->gart.pages_addr[i];
  302. for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
  303. radeon_gart_set_page(rdev, t, page_base);
  304. page_base += RADEON_GPU_PAGE_SIZE;
  305. }
  306. }
  307. mb();
  308. radeon_gart_tlb_flush(rdev);
  309. }
  310. /**
  311. * radeon_gart_init - init the driver info for managing the gart
  312. *
  313. * @rdev: radeon_device pointer
  314. *
  315. * Allocate the dummy page and init the gart driver info (all asics).
  316. * Returns 0 for success, error for failure.
  317. */
  318. int radeon_gart_init(struct radeon_device *rdev)
  319. {
  320. int r, i;
  321. if (rdev->gart.pages) {
  322. return 0;
  323. }
  324. /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
  325. if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
  326. DRM_ERROR("Page size is smaller than GPU page size!\n");
  327. return -EINVAL;
  328. }
  329. r = radeon_dummy_page_init(rdev);
  330. if (r)
  331. return r;
  332. /* Compute table size */
  333. rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
  334. rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
  335. DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
  336. rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
  337. /* Allocate pages table */
  338. rdev->gart.pages = kzalloc(sizeof(void *) * rdev->gart.num_cpu_pages,
  339. GFP_KERNEL);
  340. if (rdev->gart.pages == NULL) {
  341. radeon_gart_fini(rdev);
  342. return -ENOMEM;
  343. }
  344. rdev->gart.pages_addr = kzalloc(sizeof(dma_addr_t) *
  345. rdev->gart.num_cpu_pages, GFP_KERNEL);
  346. if (rdev->gart.pages_addr == NULL) {
  347. radeon_gart_fini(rdev);
  348. return -ENOMEM;
  349. }
  350. /* set GART entry to point to the dummy page by default */
  351. for (i = 0; i < rdev->gart.num_cpu_pages; i++) {
  352. rdev->gart.pages_addr[i] = rdev->dummy_page.addr;
  353. }
  354. return 0;
  355. }
  356. /**
  357. * radeon_gart_fini - tear down the driver info for managing the gart
  358. *
  359. * @rdev: radeon_device pointer
  360. *
  361. * Tear down the gart driver info and free the dummy page (all asics).
  362. */
  363. void radeon_gart_fini(struct radeon_device *rdev)
  364. {
  365. if (rdev->gart.pages && rdev->gart.pages_addr && rdev->gart.ready) {
  366. /* unbind pages */
  367. radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
  368. }
  369. rdev->gart.ready = false;
  370. kfree(rdev->gart.pages);
  371. kfree(rdev->gart.pages_addr);
  372. rdev->gart.pages = NULL;
  373. rdev->gart.pages_addr = NULL;
  374. radeon_dummy_page_fini(rdev);
  375. }
  376. /*
  377. * GPUVM
  378. * GPUVM is similar to the legacy gart on older asics, however
  379. * rather than there being a single global gart table
  380. * for the entire GPU, there are multiple VM page tables active
  381. * at any given time. The VM page tables can contain a mix
  382. * vram pages and system memory pages and system memory pages
  383. * can be mapped as snooped (cached system pages) or unsnooped
  384. * (uncached system pages).
  385. * Each VM has an ID associated with it and there is a page table
  386. * associated with each VMID. When execting a command buffer,
  387. * the kernel tells the the ring what VMID to use for that command
  388. * buffer. VMIDs are allocated dynamically as commands are submitted.
  389. * The userspace drivers maintain their own address space and the kernel
  390. * sets up their pages tables accordingly when they submit their
  391. * command buffers and a VMID is assigned.
  392. * Cayman/Trinity support up to 8 active VMs at any given time;
  393. * SI supports 16.
  394. */
  395. /*
  396. * vm helpers
  397. *
  398. * TODO bind a default page at vm initialization for default address
  399. */
  400. /**
  401. * radeon_vm_manager_init - init the vm manager
  402. *
  403. * @rdev: radeon_device pointer
  404. *
  405. * Init the vm manager (cayman+).
  406. * Returns 0 for success, error for failure.
  407. */
  408. int radeon_vm_manager_init(struct radeon_device *rdev)
  409. {
  410. struct radeon_vm *vm;
  411. struct radeon_bo_va *bo_va;
  412. int r;
  413. if (!rdev->vm_manager.enabled) {
  414. /* allocate enough for 2 full VM pts */
  415. r = radeon_sa_bo_manager_init(rdev, &rdev->vm_manager.sa_manager,
  416. rdev->vm_manager.max_pfn * 8 * 2,
  417. RADEON_GEM_DOMAIN_VRAM);
  418. if (r) {
  419. dev_err(rdev->dev, "failed to allocate vm bo (%dKB)\n",
  420. (rdev->vm_manager.max_pfn * 8) >> 10);
  421. return r;
  422. }
  423. r = radeon_asic_vm_init(rdev);
  424. if (r)
  425. return r;
  426. rdev->vm_manager.enabled = true;
  427. r = radeon_sa_bo_manager_start(rdev, &rdev->vm_manager.sa_manager);
  428. if (r)
  429. return r;
  430. }
  431. /* restore page table */
  432. list_for_each_entry(vm, &rdev->vm_manager.lru_vm, list) {
  433. if (vm->sa_bo == NULL)
  434. continue;
  435. list_for_each_entry(bo_va, &vm->va, vm_list) {
  436. bo_va->valid = false;
  437. }
  438. }
  439. return 0;
  440. }
  441. /**
  442. * radeon_vm_free_pt - free the page table for a specific vm
  443. *
  444. * @rdev: radeon_device pointer
  445. * @vm: vm to unbind
  446. *
  447. * Free the page table of a specific vm (cayman+).
  448. *
  449. * Global and local mutex must be lock!
  450. */
  451. static void radeon_vm_free_pt(struct radeon_device *rdev,
  452. struct radeon_vm *vm)
  453. {
  454. struct radeon_bo_va *bo_va;
  455. if (!vm->sa_bo)
  456. return;
  457. list_del_init(&vm->list);
  458. radeon_sa_bo_free(rdev, &vm->sa_bo, vm->fence);
  459. vm->pt = NULL;
  460. list_for_each_entry(bo_va, &vm->va, vm_list) {
  461. bo_va->valid = false;
  462. }
  463. }
  464. /**
  465. * radeon_vm_manager_fini - tear down the vm manager
  466. *
  467. * @rdev: radeon_device pointer
  468. *
  469. * Tear down the VM manager (cayman+).
  470. */
  471. void radeon_vm_manager_fini(struct radeon_device *rdev)
  472. {
  473. struct radeon_vm *vm, *tmp;
  474. int i;
  475. if (!rdev->vm_manager.enabled)
  476. return;
  477. mutex_lock(&rdev->vm_manager.lock);
  478. /* free all allocated page tables */
  479. list_for_each_entry_safe(vm, tmp, &rdev->vm_manager.lru_vm, list) {
  480. mutex_lock(&vm->mutex);
  481. radeon_vm_free_pt(rdev, vm);
  482. mutex_unlock(&vm->mutex);
  483. }
  484. for (i = 0; i < RADEON_NUM_VM; ++i) {
  485. radeon_fence_unref(&rdev->vm_manager.active[i]);
  486. }
  487. radeon_asic_vm_fini(rdev);
  488. mutex_unlock(&rdev->vm_manager.lock);
  489. radeon_sa_bo_manager_suspend(rdev, &rdev->vm_manager.sa_manager);
  490. radeon_sa_bo_manager_fini(rdev, &rdev->vm_manager.sa_manager);
  491. rdev->vm_manager.enabled = false;
  492. }
  493. /**
  494. * radeon_vm_alloc_pt - allocates a page table for a VM
  495. *
  496. * @rdev: radeon_device pointer
  497. * @vm: vm to bind
  498. *
  499. * Allocate a page table for the requested vm (cayman+).
  500. * Also starts to populate the page table.
  501. * Returns 0 for success, error for failure.
  502. *
  503. * Global and local mutex must be locked!
  504. */
  505. int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm)
  506. {
  507. struct radeon_vm *vm_evict;
  508. int r;
  509. if (vm == NULL) {
  510. return -EINVAL;
  511. }
  512. if (vm->sa_bo != NULL) {
  513. /* update lru */
  514. list_del_init(&vm->list);
  515. list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
  516. return 0;
  517. }
  518. retry:
  519. r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager, &vm->sa_bo,
  520. RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8),
  521. RADEON_GPU_PAGE_SIZE, false);
  522. if (r == -ENOMEM) {
  523. if (list_empty(&rdev->vm_manager.lru_vm)) {
  524. return r;
  525. }
  526. vm_evict = list_first_entry(&rdev->vm_manager.lru_vm, struct radeon_vm, list);
  527. mutex_lock(&vm_evict->mutex);
  528. radeon_vm_free_pt(rdev, vm_evict);
  529. mutex_unlock(&vm_evict->mutex);
  530. goto retry;
  531. } else if (r) {
  532. return r;
  533. }
  534. vm->pt = radeon_sa_bo_cpu_addr(vm->sa_bo);
  535. vm->pt_gpu_addr = radeon_sa_bo_gpu_addr(vm->sa_bo);
  536. memset(vm->pt, 0, RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8));
  537. list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
  538. return radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo,
  539. &rdev->ring_tmp_bo.bo->tbo.mem);
  540. }
  541. /**
  542. * radeon_vm_grab_id - allocate the next free VMID
  543. *
  544. * @rdev: radeon_device pointer
  545. * @vm: vm to allocate id for
  546. * @ring: ring we want to submit job to
  547. *
  548. * Allocate an id for the vm (cayman+).
  549. * Returns the fence we need to sync to (if any).
  550. *
  551. * Global and local mutex must be locked!
  552. */
  553. struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
  554. struct radeon_vm *vm, int ring)
  555. {
  556. struct radeon_fence *best[RADEON_NUM_RINGS] = {};
  557. unsigned choices[2] = {};
  558. unsigned i;
  559. /* check if the id is still valid */
  560. if (vm->fence && vm->fence == rdev->vm_manager.active[vm->id])
  561. return NULL;
  562. /* we definately need to flush */
  563. radeon_fence_unref(&vm->last_flush);
  564. /* skip over VMID 0, since it is the system VM */
  565. for (i = 1; i < rdev->vm_manager.nvm; ++i) {
  566. struct radeon_fence *fence = rdev->vm_manager.active[i];
  567. if (fence == NULL) {
  568. /* found a free one */
  569. vm->id = i;
  570. return NULL;
  571. }
  572. if (radeon_fence_is_earlier(fence, best[fence->ring])) {
  573. best[fence->ring] = fence;
  574. choices[fence->ring == ring ? 0 : 1] = i;
  575. }
  576. }
  577. for (i = 0; i < 2; ++i) {
  578. if (choices[i]) {
  579. vm->id = choices[i];
  580. return rdev->vm_manager.active[choices[i]];
  581. }
  582. }
  583. /* should never happen */
  584. BUG();
  585. return NULL;
  586. }
  587. /**
  588. * radeon_vm_fence - remember fence for vm
  589. *
  590. * @rdev: radeon_device pointer
  591. * @vm: vm we want to fence
  592. * @fence: fence to remember
  593. *
  594. * Fence the vm (cayman+).
  595. * Set the fence used to protect page table and id.
  596. *
  597. * Global and local mutex must be locked!
  598. */
  599. void radeon_vm_fence(struct radeon_device *rdev,
  600. struct radeon_vm *vm,
  601. struct radeon_fence *fence)
  602. {
  603. radeon_fence_unref(&rdev->vm_manager.active[vm->id]);
  604. rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence);
  605. radeon_fence_unref(&vm->fence);
  606. vm->fence = radeon_fence_ref(fence);
  607. }
  608. /**
  609. * radeon_vm_bo_find - find the bo_va for a specific vm & bo
  610. *
  611. * @vm: requested vm
  612. * @bo: requested buffer object
  613. *
  614. * Find @bo inside the requested vm (cayman+).
  615. * Search inside the @bos vm list for the requested vm
  616. * Returns the found bo_va or NULL if none is found
  617. *
  618. * Object has to be reserved!
  619. */
  620. struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
  621. struct radeon_bo *bo)
  622. {
  623. struct radeon_bo_va *bo_va;
  624. list_for_each_entry(bo_va, &bo->va, bo_list) {
  625. if (bo_va->vm == vm) {
  626. return bo_va;
  627. }
  628. }
  629. return NULL;
  630. }
  631. /**
  632. * radeon_vm_bo_add - add a bo to a specific vm
  633. *
  634. * @rdev: radeon_device pointer
  635. * @vm: requested vm
  636. * @bo: radeon buffer object
  637. *
  638. * Add @bo into the requested vm (cayman+).
  639. * Add @bo to the list of bos associated with the vm
  640. * Returns newly added bo_va or NULL for failure
  641. *
  642. * Object has to be reserved!
  643. */
  644. struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
  645. struct radeon_vm *vm,
  646. struct radeon_bo *bo)
  647. {
  648. struct radeon_bo_va *bo_va;
  649. bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
  650. if (bo_va == NULL) {
  651. return NULL;
  652. }
  653. bo_va->vm = vm;
  654. bo_va->bo = bo;
  655. bo_va->soffset = 0;
  656. bo_va->eoffset = 0;
  657. bo_va->flags = 0;
  658. bo_va->valid = false;
  659. bo_va->ref_count = 1;
  660. INIT_LIST_HEAD(&bo_va->bo_list);
  661. INIT_LIST_HEAD(&bo_va->vm_list);
  662. mutex_lock(&vm->mutex);
  663. list_add(&bo_va->vm_list, &vm->va);
  664. list_add_tail(&bo_va->bo_list, &bo->va);
  665. mutex_unlock(&vm->mutex);
  666. return bo_va;
  667. }
  668. /**
  669. * radeon_vm_bo_set_addr - set bos virtual address inside a vm
  670. *
  671. * @rdev: radeon_device pointer
  672. * @bo_va: bo_va to store the address
  673. * @soffset: requested offset of the buffer in the VM address space
  674. * @flags: attributes of pages (read/write/valid/etc.)
  675. *
  676. * Set offset of @bo_va (cayman+).
  677. * Validate and set the offset requested within the vm address space.
  678. * Returns 0 for success, error for failure.
  679. *
  680. * Object has to be reserved!
  681. */
  682. int radeon_vm_bo_set_addr(struct radeon_device *rdev,
  683. struct radeon_bo_va *bo_va,
  684. uint64_t soffset,
  685. uint32_t flags)
  686. {
  687. uint64_t size = radeon_bo_size(bo_va->bo);
  688. uint64_t eoffset, last_offset = 0;
  689. struct radeon_vm *vm = bo_va->vm;
  690. struct radeon_bo_va *tmp;
  691. struct list_head *head;
  692. unsigned last_pfn;
  693. if (soffset) {
  694. /* make sure object fit at this offset */
  695. eoffset = soffset + size;
  696. if (soffset >= eoffset) {
  697. return -EINVAL;
  698. }
  699. last_pfn = eoffset / RADEON_GPU_PAGE_SIZE;
  700. if (last_pfn > rdev->vm_manager.max_pfn) {
  701. dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n",
  702. last_pfn, rdev->vm_manager.max_pfn);
  703. return -EINVAL;
  704. }
  705. } else {
  706. eoffset = last_pfn = 0;
  707. }
  708. mutex_lock(&vm->mutex);
  709. if (last_pfn > vm->last_pfn) {
  710. /* release mutex and lock in right order */
  711. mutex_unlock(&vm->mutex);
  712. mutex_lock(&rdev->vm_manager.lock);
  713. mutex_lock(&vm->mutex);
  714. /* and check again */
  715. if (last_pfn > vm->last_pfn) {
  716. /* grow va space 32M by 32M */
  717. unsigned align = ((32 << 20) >> 12) - 1;
  718. radeon_vm_free_pt(rdev, vm);
  719. vm->last_pfn = (last_pfn + align) & ~align;
  720. }
  721. mutex_unlock(&rdev->vm_manager.lock);
  722. }
  723. head = &vm->va;
  724. last_offset = 0;
  725. list_for_each_entry(tmp, &vm->va, vm_list) {
  726. if (bo_va == tmp) {
  727. /* skip over currently modified bo */
  728. continue;
  729. }
  730. if (soffset >= last_offset && eoffset <= tmp->soffset) {
  731. /* bo can be added before this one */
  732. break;
  733. }
  734. if (eoffset > tmp->soffset && soffset < tmp->eoffset) {
  735. /* bo and tmp overlap, invalid offset */
  736. dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n",
  737. bo_va->bo, (unsigned)bo_va->soffset, tmp->bo,
  738. (unsigned)tmp->soffset, (unsigned)tmp->eoffset);
  739. mutex_unlock(&vm->mutex);
  740. return -EINVAL;
  741. }
  742. last_offset = tmp->eoffset;
  743. head = &tmp->vm_list;
  744. }
  745. bo_va->soffset = soffset;
  746. bo_va->eoffset = eoffset;
  747. bo_va->flags = flags;
  748. bo_va->valid = false;
  749. list_move(&bo_va->vm_list, head);
  750. mutex_unlock(&vm->mutex);
  751. return 0;
  752. }
  753. /**
  754. * radeon_vm_map_gart - get the physical address of a gart page
  755. *
  756. * @rdev: radeon_device pointer
  757. * @addr: the unmapped addr
  758. *
  759. * Look up the physical address of the page that the pte resolves
  760. * to (cayman+).
  761. * Returns the physical address of the page.
  762. */
  763. uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr)
  764. {
  765. uint64_t result;
  766. /* page table offset */
  767. result = rdev->gart.pages_addr[addr >> PAGE_SHIFT];
  768. /* in case cpu page size != gpu page size*/
  769. result |= addr & (~PAGE_MASK);
  770. return result;
  771. }
  772. /**
  773. * radeon_vm_bo_update_pte - map a bo into the vm page table
  774. *
  775. * @rdev: radeon_device pointer
  776. * @vm: requested vm
  777. * @bo: radeon buffer object
  778. * @mem: ttm mem
  779. *
  780. * Fill in the page table entries for @bo (cayman+).
  781. * Returns 0 for success, -EINVAL for failure.
  782. *
  783. * Object have to be reserved & global and local mutex must be locked!
  784. */
  785. int radeon_vm_bo_update_pte(struct radeon_device *rdev,
  786. struct radeon_vm *vm,
  787. struct radeon_bo *bo,
  788. struct ttm_mem_reg *mem)
  789. {
  790. unsigned ridx = rdev->asic->vm.pt_ring_index;
  791. struct radeon_ring *ring = &rdev->ring[ridx];
  792. struct radeon_semaphore *sem = NULL;
  793. struct radeon_bo_va *bo_va;
  794. unsigned ngpu_pages, ndw;
  795. uint64_t pfn, addr;
  796. int r;
  797. /* nothing to do if vm isn't bound */
  798. if (vm->sa_bo == NULL)
  799. return 0;
  800. bo_va = radeon_vm_bo_find(vm, bo);
  801. if (bo_va == NULL) {
  802. dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm);
  803. return -EINVAL;
  804. }
  805. if (!bo_va->soffset) {
  806. dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n",
  807. bo, vm);
  808. return -EINVAL;
  809. }
  810. if ((bo_va->valid && mem) || (!bo_va->valid && mem == NULL))
  811. return 0;
  812. ngpu_pages = radeon_bo_ngpu_pages(bo);
  813. bo_va->flags &= ~RADEON_VM_PAGE_VALID;
  814. bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
  815. pfn = bo_va->soffset / RADEON_GPU_PAGE_SIZE;
  816. if (mem) {
  817. addr = mem->start << PAGE_SHIFT;
  818. if (mem->mem_type != TTM_PL_SYSTEM) {
  819. bo_va->flags |= RADEON_VM_PAGE_VALID;
  820. bo_va->valid = true;
  821. }
  822. if (mem->mem_type == TTM_PL_TT) {
  823. bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
  824. } else {
  825. addr += rdev->vm_manager.vram_base_offset;
  826. }
  827. } else {
  828. addr = 0;
  829. bo_va->valid = false;
  830. }
  831. if (vm->fence && radeon_fence_signaled(vm->fence)) {
  832. radeon_fence_unref(&vm->fence);
  833. }
  834. if (vm->fence && vm->fence->ring != ridx) {
  835. r = radeon_semaphore_create(rdev, &sem);
  836. if (r) {
  837. return r;
  838. }
  839. }
  840. /* estimate number of dw needed */
  841. ndw = 32;
  842. ndw += (ngpu_pages >> 12) * 3;
  843. ndw += ngpu_pages * 2;
  844. r = radeon_ring_lock(rdev, ring, ndw);
  845. if (r) {
  846. return r;
  847. }
  848. if (sem && radeon_fence_need_sync(vm->fence, ridx)) {
  849. radeon_semaphore_sync_rings(rdev, sem, vm->fence->ring, ridx);
  850. radeon_fence_note_sync(vm->fence, ridx);
  851. }
  852. radeon_asic_vm_set_page(rdev, vm->pt_gpu_addr + pfn * 8, addr,
  853. ngpu_pages, RADEON_GPU_PAGE_SIZE, bo_va->flags);
  854. radeon_fence_unref(&vm->fence);
  855. r = radeon_fence_emit(rdev, &vm->fence, ridx);
  856. if (r) {
  857. radeon_ring_unlock_undo(rdev, ring);
  858. return r;
  859. }
  860. radeon_ring_unlock_commit(rdev, ring);
  861. radeon_semaphore_free(rdev, &sem, vm->fence);
  862. radeon_fence_unref(&vm->last_flush);
  863. return 0;
  864. }
  865. /**
  866. * radeon_vm_bo_rmv - remove a bo to a specific vm
  867. *
  868. * @rdev: radeon_device pointer
  869. * @bo_va: requested bo_va
  870. *
  871. * Remove @bo_va->bo from the requested vm (cayman+).
  872. * Remove @bo_va->bo from the list of bos associated with the bo_va->vm and
  873. * remove the ptes for @bo_va in the page table.
  874. * Returns 0 for success.
  875. *
  876. * Object have to be reserved!
  877. */
  878. int radeon_vm_bo_rmv(struct radeon_device *rdev,
  879. struct radeon_bo_va *bo_va)
  880. {
  881. int r;
  882. mutex_lock(&rdev->vm_manager.lock);
  883. mutex_lock(&bo_va->vm->mutex);
  884. r = radeon_vm_bo_update_pte(rdev, bo_va->vm, bo_va->bo, NULL);
  885. mutex_unlock(&rdev->vm_manager.lock);
  886. list_del(&bo_va->vm_list);
  887. mutex_unlock(&bo_va->vm->mutex);
  888. list_del(&bo_va->bo_list);
  889. kfree(bo_va);
  890. return r;
  891. }
  892. /**
  893. * radeon_vm_bo_invalidate - mark the bo as invalid
  894. *
  895. * @rdev: radeon_device pointer
  896. * @vm: requested vm
  897. * @bo: radeon buffer object
  898. *
  899. * Mark @bo as invalid (cayman+).
  900. */
  901. void radeon_vm_bo_invalidate(struct radeon_device *rdev,
  902. struct radeon_bo *bo)
  903. {
  904. struct radeon_bo_va *bo_va;
  905. BUG_ON(!atomic_read(&bo->tbo.reserved));
  906. list_for_each_entry(bo_va, &bo->va, bo_list) {
  907. bo_va->valid = false;
  908. }
  909. }
  910. /**
  911. * radeon_vm_init - initialize a vm instance
  912. *
  913. * @rdev: radeon_device pointer
  914. * @vm: requested vm
  915. *
  916. * Init @vm (cayman+).
  917. * Map the IB pool and any other shared objects into the VM
  918. * by default as it's used by all VMs.
  919. * Returns 0 for success, error for failure.
  920. */
  921. int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
  922. {
  923. struct radeon_bo_va *bo_va;
  924. int r;
  925. vm->id = 0;
  926. vm->fence = NULL;
  927. mutex_init(&vm->mutex);
  928. INIT_LIST_HEAD(&vm->list);
  929. INIT_LIST_HEAD(&vm->va);
  930. /* SI requires equal sized PTs for all VMs, so always set
  931. * last_pfn to max_pfn. cayman allows variable sized
  932. * pts so we can grow then as needed. Once we switch
  933. * to two level pts we can unify this again.
  934. */
  935. if (rdev->family >= CHIP_TAHITI)
  936. vm->last_pfn = rdev->vm_manager.max_pfn;
  937. else
  938. vm->last_pfn = 0;
  939. /* map the ib pool buffer at 0 in virtual address space, set
  940. * read only
  941. */
  942. bo_va = radeon_vm_bo_add(rdev, vm, rdev->ring_tmp_bo.bo);
  943. r = radeon_vm_bo_set_addr(rdev, bo_va, RADEON_VA_IB_OFFSET,
  944. RADEON_VM_PAGE_READABLE |
  945. RADEON_VM_PAGE_SNOOPED);
  946. return r;
  947. }
  948. /**
  949. * radeon_vm_fini - tear down a vm instance
  950. *
  951. * @rdev: radeon_device pointer
  952. * @vm: requested vm
  953. *
  954. * Tear down @vm (cayman+).
  955. * Unbind the VM and remove all bos from the vm bo list
  956. */
  957. void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
  958. {
  959. struct radeon_bo_va *bo_va, *tmp;
  960. int r;
  961. mutex_lock(&rdev->vm_manager.lock);
  962. mutex_lock(&vm->mutex);
  963. radeon_vm_free_pt(rdev, vm);
  964. mutex_unlock(&rdev->vm_manager.lock);
  965. /* remove all bo at this point non are busy any more because unbind
  966. * waited for the last vm fence to signal
  967. */
  968. r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
  969. if (!r) {
  970. bo_va = radeon_vm_bo_find(vm, rdev->ring_tmp_bo.bo);
  971. list_del_init(&bo_va->bo_list);
  972. list_del_init(&bo_va->vm_list);
  973. radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
  974. kfree(bo_va);
  975. }
  976. if (!list_empty(&vm->va)) {
  977. dev_err(rdev->dev, "still active bo inside vm\n");
  978. }
  979. list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) {
  980. list_del_init(&bo_va->vm_list);
  981. r = radeon_bo_reserve(bo_va->bo, false);
  982. if (!r) {
  983. list_del_init(&bo_va->bo_list);
  984. radeon_bo_unreserve(bo_va->bo);
  985. kfree(bo_va);
  986. }
  987. }
  988. radeon_fence_unref(&vm->fence);
  989. radeon_fence_unref(&vm->last_flush);
  990. mutex_unlock(&vm->mutex);
  991. }