radeon_gart.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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_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_directory_size - returns the size of the page directory in bytes
  402. *
  403. * @rdev: radeon_device pointer
  404. *
  405. * Calculate the size of the page directory in bytes (cayman+).
  406. */
  407. static unsigned radeon_vm_directory_size(struct radeon_device *rdev)
  408. {
  409. return (rdev->vm_manager.max_pfn >> RADEON_VM_BLOCK_SIZE) * 8;
  410. }
  411. /**
  412. * radeon_vm_manager_init - init the vm manager
  413. *
  414. * @rdev: radeon_device pointer
  415. *
  416. * Init the vm manager (cayman+).
  417. * Returns 0 for success, error for failure.
  418. */
  419. int radeon_vm_manager_init(struct radeon_device *rdev)
  420. {
  421. struct radeon_vm *vm;
  422. struct radeon_bo_va *bo_va;
  423. int r;
  424. unsigned size;
  425. if (!rdev->vm_manager.enabled) {
  426. /* allocate enough for 2 full VM pts */
  427. size = RADEON_GPU_PAGE_ALIGN(radeon_vm_directory_size(rdev));
  428. size += RADEON_GPU_PAGE_ALIGN(rdev->vm_manager.max_pfn * 8);
  429. size *= 2;
  430. r = radeon_sa_bo_manager_init(rdev, &rdev->vm_manager.sa_manager,
  431. size,
  432. RADEON_GEM_DOMAIN_VRAM);
  433. if (r) {
  434. dev_err(rdev->dev, "failed to allocate vm bo (%dKB)\n",
  435. (rdev->vm_manager.max_pfn * 8) >> 10);
  436. return r;
  437. }
  438. r = radeon_asic_vm_init(rdev);
  439. if (r)
  440. return r;
  441. rdev->vm_manager.enabled = true;
  442. r = radeon_sa_bo_manager_start(rdev, &rdev->vm_manager.sa_manager);
  443. if (r)
  444. return r;
  445. }
  446. /* restore page table */
  447. list_for_each_entry(vm, &rdev->vm_manager.lru_vm, list) {
  448. if (vm->sa_bo == NULL)
  449. continue;
  450. list_for_each_entry(bo_va, &vm->va, vm_list) {
  451. bo_va->valid = false;
  452. }
  453. }
  454. return 0;
  455. }
  456. /**
  457. * radeon_vm_free_pt - free the page table for a specific vm
  458. *
  459. * @rdev: radeon_device pointer
  460. * @vm: vm to unbind
  461. *
  462. * Free the page table of a specific vm (cayman+).
  463. *
  464. * Global and local mutex must be lock!
  465. */
  466. static void radeon_vm_free_pt(struct radeon_device *rdev,
  467. struct radeon_vm *vm)
  468. {
  469. struct radeon_bo_va *bo_va;
  470. if (!vm->sa_bo)
  471. return;
  472. list_del_init(&vm->list);
  473. radeon_sa_bo_free(rdev, &vm->sa_bo, vm->fence);
  474. list_for_each_entry(bo_va, &vm->va, vm_list) {
  475. bo_va->valid = false;
  476. }
  477. }
  478. /**
  479. * radeon_vm_manager_fini - tear down the vm manager
  480. *
  481. * @rdev: radeon_device pointer
  482. *
  483. * Tear down the VM manager (cayman+).
  484. */
  485. void radeon_vm_manager_fini(struct radeon_device *rdev)
  486. {
  487. struct radeon_vm *vm, *tmp;
  488. int i;
  489. if (!rdev->vm_manager.enabled)
  490. return;
  491. mutex_lock(&rdev->vm_manager.lock);
  492. /* free all allocated page tables */
  493. list_for_each_entry_safe(vm, tmp, &rdev->vm_manager.lru_vm, list) {
  494. mutex_lock(&vm->mutex);
  495. radeon_vm_free_pt(rdev, vm);
  496. mutex_unlock(&vm->mutex);
  497. }
  498. for (i = 0; i < RADEON_NUM_VM; ++i) {
  499. radeon_fence_unref(&rdev->vm_manager.active[i]);
  500. }
  501. radeon_asic_vm_fini(rdev);
  502. mutex_unlock(&rdev->vm_manager.lock);
  503. radeon_sa_bo_manager_suspend(rdev, &rdev->vm_manager.sa_manager);
  504. radeon_sa_bo_manager_fini(rdev, &rdev->vm_manager.sa_manager);
  505. rdev->vm_manager.enabled = false;
  506. }
  507. /**
  508. * radeon_vm_alloc_pt - allocates a page table for a VM
  509. *
  510. * @rdev: radeon_device pointer
  511. * @vm: vm to bind
  512. *
  513. * Allocate a page table for the requested vm (cayman+).
  514. * Also starts to populate the page table.
  515. * Returns 0 for success, error for failure.
  516. *
  517. * Global and local mutex must be locked!
  518. */
  519. int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm)
  520. {
  521. struct radeon_vm *vm_evict;
  522. int r;
  523. u64 *pd_addr;
  524. int tables_size;
  525. if (vm == NULL) {
  526. return -EINVAL;
  527. }
  528. /* allocate enough to cover the current VM size */
  529. tables_size = RADEON_GPU_PAGE_ALIGN(radeon_vm_directory_size(rdev));
  530. tables_size += RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8);
  531. if (vm->sa_bo != NULL) {
  532. /* update lru */
  533. list_del_init(&vm->list);
  534. list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
  535. return 0;
  536. }
  537. retry:
  538. r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager, &vm->sa_bo,
  539. tables_size, RADEON_GPU_PAGE_SIZE, false);
  540. if (r == -ENOMEM) {
  541. if (list_empty(&rdev->vm_manager.lru_vm)) {
  542. return r;
  543. }
  544. vm_evict = list_first_entry(&rdev->vm_manager.lru_vm, struct radeon_vm, list);
  545. mutex_lock(&vm_evict->mutex);
  546. radeon_vm_free_pt(rdev, vm_evict);
  547. mutex_unlock(&vm_evict->mutex);
  548. goto retry;
  549. } else if (r) {
  550. return r;
  551. }
  552. pd_addr = radeon_sa_bo_cpu_addr(vm->sa_bo);
  553. vm->pd_gpu_addr = radeon_sa_bo_gpu_addr(vm->sa_bo);
  554. memset(pd_addr, 0, tables_size);
  555. list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
  556. return radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo,
  557. &rdev->ring_tmp_bo.bo->tbo.mem);
  558. }
  559. /**
  560. * radeon_vm_grab_id - allocate the next free VMID
  561. *
  562. * @rdev: radeon_device pointer
  563. * @vm: vm to allocate id for
  564. * @ring: ring we want to submit job to
  565. *
  566. * Allocate an id for the vm (cayman+).
  567. * Returns the fence we need to sync to (if any).
  568. *
  569. * Global and local mutex must be locked!
  570. */
  571. struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
  572. struct radeon_vm *vm, int ring)
  573. {
  574. struct radeon_fence *best[RADEON_NUM_RINGS] = {};
  575. unsigned choices[2] = {};
  576. unsigned i;
  577. /* check if the id is still valid */
  578. if (vm->fence && vm->fence == rdev->vm_manager.active[vm->id])
  579. return NULL;
  580. /* we definately need to flush */
  581. radeon_fence_unref(&vm->last_flush);
  582. /* skip over VMID 0, since it is the system VM */
  583. for (i = 1; i < rdev->vm_manager.nvm; ++i) {
  584. struct radeon_fence *fence = rdev->vm_manager.active[i];
  585. if (fence == NULL) {
  586. /* found a free one */
  587. vm->id = i;
  588. return NULL;
  589. }
  590. if (radeon_fence_is_earlier(fence, best[fence->ring])) {
  591. best[fence->ring] = fence;
  592. choices[fence->ring == ring ? 0 : 1] = i;
  593. }
  594. }
  595. for (i = 0; i < 2; ++i) {
  596. if (choices[i]) {
  597. vm->id = choices[i];
  598. return rdev->vm_manager.active[choices[i]];
  599. }
  600. }
  601. /* should never happen */
  602. BUG();
  603. return NULL;
  604. }
  605. /**
  606. * radeon_vm_fence - remember fence for vm
  607. *
  608. * @rdev: radeon_device pointer
  609. * @vm: vm we want to fence
  610. * @fence: fence to remember
  611. *
  612. * Fence the vm (cayman+).
  613. * Set the fence used to protect page table and id.
  614. *
  615. * Global and local mutex must be locked!
  616. */
  617. void radeon_vm_fence(struct radeon_device *rdev,
  618. struct radeon_vm *vm,
  619. struct radeon_fence *fence)
  620. {
  621. radeon_fence_unref(&rdev->vm_manager.active[vm->id]);
  622. rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence);
  623. radeon_fence_unref(&vm->fence);
  624. vm->fence = radeon_fence_ref(fence);
  625. }
  626. /**
  627. * radeon_vm_bo_find - find the bo_va for a specific vm & bo
  628. *
  629. * @vm: requested vm
  630. * @bo: requested buffer object
  631. *
  632. * Find @bo inside the requested vm (cayman+).
  633. * Search inside the @bos vm list for the requested vm
  634. * Returns the found bo_va or NULL if none is found
  635. *
  636. * Object has to be reserved!
  637. */
  638. struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
  639. struct radeon_bo *bo)
  640. {
  641. struct radeon_bo_va *bo_va;
  642. list_for_each_entry(bo_va, &bo->va, bo_list) {
  643. if (bo_va->vm == vm) {
  644. return bo_va;
  645. }
  646. }
  647. return NULL;
  648. }
  649. /**
  650. * radeon_vm_bo_add - add a bo to a specific vm
  651. *
  652. * @rdev: radeon_device pointer
  653. * @vm: requested vm
  654. * @bo: radeon buffer object
  655. *
  656. * Add @bo into the requested vm (cayman+).
  657. * Add @bo to the list of bos associated with the vm
  658. * Returns newly added bo_va or NULL for failure
  659. *
  660. * Object has to be reserved!
  661. */
  662. struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
  663. struct radeon_vm *vm,
  664. struct radeon_bo *bo)
  665. {
  666. struct radeon_bo_va *bo_va;
  667. bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
  668. if (bo_va == NULL) {
  669. return NULL;
  670. }
  671. bo_va->vm = vm;
  672. bo_va->bo = bo;
  673. bo_va->soffset = 0;
  674. bo_va->eoffset = 0;
  675. bo_va->flags = 0;
  676. bo_va->valid = false;
  677. bo_va->ref_count = 1;
  678. INIT_LIST_HEAD(&bo_va->bo_list);
  679. INIT_LIST_HEAD(&bo_va->vm_list);
  680. mutex_lock(&vm->mutex);
  681. list_add(&bo_va->vm_list, &vm->va);
  682. list_add_tail(&bo_va->bo_list, &bo->va);
  683. mutex_unlock(&vm->mutex);
  684. return bo_va;
  685. }
  686. /**
  687. * radeon_vm_bo_set_addr - set bos virtual address inside a vm
  688. *
  689. * @rdev: radeon_device pointer
  690. * @bo_va: bo_va to store the address
  691. * @soffset: requested offset of the buffer in the VM address space
  692. * @flags: attributes of pages (read/write/valid/etc.)
  693. *
  694. * Set offset of @bo_va (cayman+).
  695. * Validate and set the offset requested within the vm address space.
  696. * Returns 0 for success, error for failure.
  697. *
  698. * Object has to be reserved!
  699. */
  700. int radeon_vm_bo_set_addr(struct radeon_device *rdev,
  701. struct radeon_bo_va *bo_va,
  702. uint64_t soffset,
  703. uint32_t flags)
  704. {
  705. uint64_t size = radeon_bo_size(bo_va->bo);
  706. uint64_t eoffset, last_offset = 0;
  707. struct radeon_vm *vm = bo_va->vm;
  708. struct radeon_bo_va *tmp;
  709. struct list_head *head;
  710. unsigned last_pfn;
  711. if (soffset) {
  712. /* make sure object fit at this offset */
  713. eoffset = soffset + size;
  714. if (soffset >= eoffset) {
  715. return -EINVAL;
  716. }
  717. last_pfn = eoffset / RADEON_GPU_PAGE_SIZE;
  718. if (last_pfn > rdev->vm_manager.max_pfn) {
  719. dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n",
  720. last_pfn, rdev->vm_manager.max_pfn);
  721. return -EINVAL;
  722. }
  723. } else {
  724. eoffset = last_pfn = 0;
  725. }
  726. mutex_lock(&vm->mutex);
  727. if (last_pfn > vm->last_pfn) {
  728. /* release mutex and lock in right order */
  729. mutex_unlock(&vm->mutex);
  730. mutex_lock(&rdev->vm_manager.lock);
  731. mutex_lock(&vm->mutex);
  732. /* and check again */
  733. if (last_pfn > vm->last_pfn) {
  734. /* grow va space 32M by 32M */
  735. unsigned align = ((32 << 20) >> 12) - 1;
  736. radeon_vm_free_pt(rdev, vm);
  737. vm->last_pfn = (last_pfn + align) & ~align;
  738. }
  739. mutex_unlock(&rdev->vm_manager.lock);
  740. }
  741. head = &vm->va;
  742. last_offset = 0;
  743. list_for_each_entry(tmp, &vm->va, vm_list) {
  744. if (bo_va == tmp) {
  745. /* skip over currently modified bo */
  746. continue;
  747. }
  748. if (soffset >= last_offset && eoffset <= tmp->soffset) {
  749. /* bo can be added before this one */
  750. break;
  751. }
  752. if (eoffset > tmp->soffset && soffset < tmp->eoffset) {
  753. /* bo and tmp overlap, invalid offset */
  754. dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n",
  755. bo_va->bo, (unsigned)bo_va->soffset, tmp->bo,
  756. (unsigned)tmp->soffset, (unsigned)tmp->eoffset);
  757. mutex_unlock(&vm->mutex);
  758. return -EINVAL;
  759. }
  760. last_offset = tmp->eoffset;
  761. head = &tmp->vm_list;
  762. }
  763. bo_va->soffset = soffset;
  764. bo_va->eoffset = eoffset;
  765. bo_va->flags = flags;
  766. bo_va->valid = false;
  767. list_move(&bo_va->vm_list, head);
  768. mutex_unlock(&vm->mutex);
  769. return 0;
  770. }
  771. /**
  772. * radeon_vm_map_gart - get the physical address of a gart page
  773. *
  774. * @rdev: radeon_device pointer
  775. * @addr: the unmapped addr
  776. *
  777. * Look up the physical address of the page that the pte resolves
  778. * to (cayman+).
  779. * Returns the physical address of the page.
  780. */
  781. uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr)
  782. {
  783. uint64_t result;
  784. /* page table offset */
  785. result = rdev->gart.pages_addr[addr >> PAGE_SHIFT];
  786. /* in case cpu page size != gpu page size*/
  787. result |= addr & (~PAGE_MASK);
  788. return result;
  789. }
  790. /**
  791. * radeon_vm_bo_update_pte - map a bo into the vm page table
  792. *
  793. * @rdev: radeon_device pointer
  794. * @vm: requested vm
  795. * @bo: radeon buffer object
  796. * @mem: ttm mem
  797. *
  798. * Fill in the page table entries for @bo (cayman+).
  799. * Returns 0 for success, -EINVAL for failure.
  800. *
  801. * Object have to be reserved & global and local mutex must be locked!
  802. */
  803. int radeon_vm_bo_update_pte(struct radeon_device *rdev,
  804. struct radeon_vm *vm,
  805. struct radeon_bo *bo,
  806. struct ttm_mem_reg *mem)
  807. {
  808. unsigned ridx = rdev->asic->vm.pt_ring_index;
  809. struct radeon_ring *ring = &rdev->ring[ridx];
  810. struct radeon_semaphore *sem = NULL;
  811. struct radeon_bo_va *bo_va;
  812. unsigned nptes, npdes, ndw;
  813. uint64_t pe, addr;
  814. uint64_t pfn;
  815. int r;
  816. /* nothing to do if vm isn't bound */
  817. if (vm->sa_bo == NULL)
  818. return 0;
  819. bo_va = radeon_vm_bo_find(vm, bo);
  820. if (bo_va == NULL) {
  821. dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm);
  822. return -EINVAL;
  823. }
  824. if (!bo_va->soffset) {
  825. dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n",
  826. bo, vm);
  827. return -EINVAL;
  828. }
  829. if ((bo_va->valid && mem) || (!bo_va->valid && mem == NULL))
  830. return 0;
  831. bo_va->flags &= ~RADEON_VM_PAGE_VALID;
  832. bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
  833. if (mem) {
  834. addr = mem->start << PAGE_SHIFT;
  835. if (mem->mem_type != TTM_PL_SYSTEM) {
  836. bo_va->flags |= RADEON_VM_PAGE_VALID;
  837. bo_va->valid = true;
  838. }
  839. if (mem->mem_type == TTM_PL_TT) {
  840. bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
  841. } else {
  842. addr += rdev->vm_manager.vram_base_offset;
  843. }
  844. } else {
  845. addr = 0;
  846. bo_va->valid = false;
  847. }
  848. if (vm->fence && radeon_fence_signaled(vm->fence)) {
  849. radeon_fence_unref(&vm->fence);
  850. }
  851. if (vm->fence && vm->fence->ring != ridx) {
  852. r = radeon_semaphore_create(rdev, &sem);
  853. if (r) {
  854. return r;
  855. }
  856. }
  857. /* estimate number of dw needed */
  858. /* reserve space for 32-bit padding */
  859. ndw = 32;
  860. nptes = radeon_bo_ngpu_pages(bo);
  861. pfn = (bo_va->soffset / RADEON_GPU_PAGE_SIZE);
  862. /* handle cases where a bo spans several pdes */
  863. npdes = (ALIGN(pfn + nptes, RADEON_VM_PTE_COUNT) -
  864. (pfn & ~(RADEON_VM_PTE_COUNT - 1))) >> RADEON_VM_BLOCK_SIZE;
  865. /* reserve space for one header for every 2k dwords */
  866. ndw += (nptes >> 11) * 3;
  867. /* reserve space for pte addresses */
  868. ndw += nptes * 2;
  869. /* reserve space for one header for every 2k dwords */
  870. ndw += (npdes >> 11) * 3;
  871. /* reserve space for pde addresses */
  872. ndw += npdes * 2;
  873. r = radeon_ring_lock(rdev, ring, ndw);
  874. if (r) {
  875. return r;
  876. }
  877. if (sem && radeon_fence_need_sync(vm->fence, ridx)) {
  878. radeon_semaphore_sync_rings(rdev, sem, vm->fence->ring, ridx);
  879. radeon_fence_note_sync(vm->fence, ridx);
  880. }
  881. /* update page table entries */
  882. pe = vm->pd_gpu_addr;
  883. pe += radeon_vm_directory_size(rdev);
  884. pe += (bo_va->soffset / RADEON_GPU_PAGE_SIZE) * 8;
  885. radeon_asic_vm_set_page(rdev, pe, addr, nptes,
  886. RADEON_GPU_PAGE_SIZE, bo_va->flags);
  887. /* update page directory entries */
  888. addr = pe;
  889. pe = vm->pd_gpu_addr;
  890. pe += ((bo_va->soffset / RADEON_GPU_PAGE_SIZE) >> RADEON_VM_BLOCK_SIZE) * 8;
  891. radeon_asic_vm_set_page(rdev, pe, addr, npdes,
  892. RADEON_VM_PTE_COUNT * 8, RADEON_VM_PAGE_VALID);
  893. radeon_fence_unref(&vm->fence);
  894. r = radeon_fence_emit(rdev, &vm->fence, ridx);
  895. if (r) {
  896. radeon_ring_unlock_undo(rdev, ring);
  897. return r;
  898. }
  899. radeon_ring_unlock_commit(rdev, ring);
  900. radeon_semaphore_free(rdev, &sem, vm->fence);
  901. radeon_fence_unref(&vm->last_flush);
  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. * Remove @bo_va->bo from the list of bos associated with the bo_va->vm and
  912. * remove the ptes for @bo_va in the page table.
  913. * Returns 0 for success.
  914. *
  915. * Object have to be reserved!
  916. */
  917. int radeon_vm_bo_rmv(struct radeon_device *rdev,
  918. struct radeon_bo_va *bo_va)
  919. {
  920. int r;
  921. mutex_lock(&rdev->vm_manager.lock);
  922. mutex_lock(&bo_va->vm->mutex);
  923. r = radeon_vm_bo_update_pte(rdev, bo_va->vm, bo_va->bo, NULL);
  924. mutex_unlock(&rdev->vm_manager.lock);
  925. list_del(&bo_va->vm_list);
  926. mutex_unlock(&bo_va->vm->mutex);
  927. list_del(&bo_va->bo_list);
  928. kfree(bo_va);
  929. return r;
  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. BUG_ON(!atomic_read(&bo->tbo.reserved));
  945. list_for_each_entry(bo_va, &bo->va, bo_list) {
  946. bo_va->valid = false;
  947. }
  948. }
  949. /**
  950. * radeon_vm_init - initialize a vm instance
  951. *
  952. * @rdev: radeon_device pointer
  953. * @vm: requested vm
  954. *
  955. * Init @vm (cayman+).
  956. * Map the IB pool and any other shared objects into the VM
  957. * by default as it's used by all VMs.
  958. * Returns 0 for success, error for failure.
  959. */
  960. int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
  961. {
  962. struct radeon_bo_va *bo_va;
  963. int r;
  964. vm->id = 0;
  965. vm->fence = NULL;
  966. vm->last_pfn = 0;
  967. mutex_init(&vm->mutex);
  968. INIT_LIST_HEAD(&vm->list);
  969. INIT_LIST_HEAD(&vm->va);
  970. /* map the ib pool buffer at 0 in virtual address space, set
  971. * read only
  972. */
  973. bo_va = radeon_vm_bo_add(rdev, vm, rdev->ring_tmp_bo.bo);
  974. r = radeon_vm_bo_set_addr(rdev, bo_va, RADEON_VA_IB_OFFSET,
  975. RADEON_VM_PAGE_READABLE |
  976. RADEON_VM_PAGE_SNOOPED);
  977. return r;
  978. }
  979. /**
  980. * radeon_vm_fini - tear down a vm instance
  981. *
  982. * @rdev: radeon_device pointer
  983. * @vm: requested vm
  984. *
  985. * Tear down @vm (cayman+).
  986. * Unbind the VM and remove all bos from the vm bo list
  987. */
  988. void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
  989. {
  990. struct radeon_bo_va *bo_va, *tmp;
  991. int r;
  992. mutex_lock(&rdev->vm_manager.lock);
  993. mutex_lock(&vm->mutex);
  994. radeon_vm_free_pt(rdev, vm);
  995. mutex_unlock(&rdev->vm_manager.lock);
  996. /* remove all bo at this point non are busy any more because unbind
  997. * waited for the last vm fence to signal
  998. */
  999. r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
  1000. if (!r) {
  1001. bo_va = radeon_vm_bo_find(vm, rdev->ring_tmp_bo.bo);
  1002. list_del_init(&bo_va->bo_list);
  1003. list_del_init(&bo_va->vm_list);
  1004. radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
  1005. kfree(bo_va);
  1006. }
  1007. if (!list_empty(&vm->va)) {
  1008. dev_err(rdev->dev, "still active bo inside vm\n");
  1009. }
  1010. list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) {
  1011. list_del_init(&bo_va->vm_list);
  1012. r = radeon_bo_reserve(bo_va->bo, false);
  1013. if (!r) {
  1014. list_del_init(&bo_va->bo_list);
  1015. radeon_bo_unreserve(bo_va->bo);
  1016. kfree(bo_va);
  1017. }
  1018. }
  1019. radeon_fence_unref(&vm->fence);
  1020. radeon_fence_unref(&vm->last_flush);
  1021. mutex_unlock(&vm->mutex);
  1022. }