nouveau_vm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_mm.h"
  27. #include "nouveau_vm.h"
  28. void
  29. nouveau_vm_map_at(struct nouveau_vma *vma, u64 delta, struct nouveau_mem *node)
  30. {
  31. struct nouveau_vm *vm = vma->vm;
  32. struct nouveau_mm_node *r;
  33. int big = vma->node->type != vm->spg_shift;
  34. u32 offset = vma->node->offset + (delta >> 12);
  35. u32 bits = vma->node->type - 12;
  36. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  37. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  38. u32 max = 1 << (vm->pgt_bits - bits);
  39. u32 end, len;
  40. delta = 0;
  41. list_for_each_entry(r, &node->regions, rl_entry) {
  42. u64 phys = (u64)r->offset << 12;
  43. u32 num = r->length >> bits;
  44. while (num) {
  45. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  46. end = (pte + num);
  47. if (unlikely(end >= max))
  48. end = max;
  49. len = end - pte;
  50. vm->map(vma, pgt, node, pte, len, phys, delta);
  51. num -= len;
  52. pte += len;
  53. if (unlikely(end >= max)) {
  54. phys += len << (bits + 12);
  55. pde++;
  56. pte = 0;
  57. }
  58. delta += (u64)len << vma->node->type;
  59. }
  60. }
  61. vm->flush(vm);
  62. }
  63. void
  64. nouveau_vm_map(struct nouveau_vma *vma, struct nouveau_mem *node)
  65. {
  66. nouveau_vm_map_at(vma, 0, node);
  67. }
  68. void
  69. nouveau_vm_map_sg_table(struct nouveau_vma *vma, u64 delta, u64 length,
  70. struct nouveau_mem *mem)
  71. {
  72. struct nouveau_vm *vm = vma->vm;
  73. int big = vma->node->type != vm->spg_shift;
  74. u32 offset = vma->node->offset + (delta >> 12);
  75. u32 bits = vma->node->type - 12;
  76. u32 num = length >> vma->node->type;
  77. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  78. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  79. u32 max = 1 << (vm->pgt_bits - bits);
  80. unsigned m, sglen;
  81. u32 end, len;
  82. int i;
  83. struct scatterlist *sg;
  84. for_each_sg(mem->sg->sgl, sg, mem->sg->nents, i) {
  85. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  86. sglen = sg_dma_len(sg) >> PAGE_SHIFT;
  87. end = pte + sglen;
  88. if (unlikely(end >= max))
  89. end = max;
  90. len = end - pte;
  91. for (m = 0; m < len; m++) {
  92. dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
  93. vm->map_sg(vma, pgt, mem, pte, 1, &addr);
  94. num--;
  95. pte++;
  96. if (num == 0)
  97. goto finish;
  98. }
  99. if (unlikely(end >= max)) {
  100. pde++;
  101. pte = 0;
  102. }
  103. if (m < sglen) {
  104. for (; m < sglen; m++) {
  105. dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
  106. vm->map_sg(vma, pgt, mem, pte, 1, &addr);
  107. num--;
  108. pte++;
  109. if (num == 0)
  110. goto finish;
  111. }
  112. }
  113. }
  114. finish:
  115. vm->flush(vm);
  116. }
  117. void
  118. nouveau_vm_map_sg(struct nouveau_vma *vma, u64 delta, u64 length,
  119. struct nouveau_mem *mem)
  120. {
  121. struct nouveau_vm *vm = vma->vm;
  122. dma_addr_t *list = mem->pages;
  123. int big = vma->node->type != vm->spg_shift;
  124. u32 offset = vma->node->offset + (delta >> 12);
  125. u32 bits = vma->node->type - 12;
  126. u32 num = length >> vma->node->type;
  127. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  128. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  129. u32 max = 1 << (vm->pgt_bits - bits);
  130. u32 end, len;
  131. while (num) {
  132. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  133. end = (pte + num);
  134. if (unlikely(end >= max))
  135. end = max;
  136. len = end - pte;
  137. vm->map_sg(vma, pgt, mem, pte, len, list);
  138. num -= len;
  139. pte += len;
  140. list += len;
  141. if (unlikely(end >= max)) {
  142. pde++;
  143. pte = 0;
  144. }
  145. }
  146. vm->flush(vm);
  147. }
  148. void
  149. nouveau_vm_unmap_at(struct nouveau_vma *vma, u64 delta, u64 length)
  150. {
  151. struct nouveau_vm *vm = vma->vm;
  152. int big = vma->node->type != vm->spg_shift;
  153. u32 offset = vma->node->offset + (delta >> 12);
  154. u32 bits = vma->node->type - 12;
  155. u32 num = length >> vma->node->type;
  156. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  157. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  158. u32 max = 1 << (vm->pgt_bits - bits);
  159. u32 end, len;
  160. while (num) {
  161. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  162. end = (pte + num);
  163. if (unlikely(end >= max))
  164. end = max;
  165. len = end - pte;
  166. vm->unmap(pgt, pte, len);
  167. num -= len;
  168. pte += len;
  169. if (unlikely(end >= max)) {
  170. pde++;
  171. pte = 0;
  172. }
  173. }
  174. vm->flush(vm);
  175. }
  176. void
  177. nouveau_vm_unmap(struct nouveau_vma *vma)
  178. {
  179. nouveau_vm_unmap_at(vma, 0, (u64)vma->node->length << 12);
  180. }
  181. static void
  182. nouveau_vm_unmap_pgt(struct nouveau_vm *vm, int big, u32 fpde, u32 lpde)
  183. {
  184. struct nouveau_vm_pgd *vpgd;
  185. struct nouveau_vm_pgt *vpgt;
  186. struct nouveau_gpuobj *pgt;
  187. u32 pde;
  188. for (pde = fpde; pde <= lpde; pde++) {
  189. vpgt = &vm->pgt[pde - vm->fpde];
  190. if (--vpgt->refcount[big])
  191. continue;
  192. pgt = vpgt->obj[big];
  193. vpgt->obj[big] = NULL;
  194. list_for_each_entry(vpgd, &vm->pgd_list, head) {
  195. vm->map_pgt(vpgd->obj, pde, vpgt->obj);
  196. }
  197. mutex_unlock(&vm->mm.mutex);
  198. nouveau_gpuobj_ref(NULL, &pgt);
  199. mutex_lock(&vm->mm.mutex);
  200. }
  201. }
  202. static int
  203. nouveau_vm_map_pgt(struct nouveau_vm *vm, u32 pde, u32 type)
  204. {
  205. struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
  206. struct nouveau_vm_pgd *vpgd;
  207. struct nouveau_gpuobj *pgt;
  208. int big = (type != vm->spg_shift);
  209. u32 pgt_size;
  210. int ret;
  211. pgt_size = (1 << (vm->pgt_bits + 12)) >> type;
  212. pgt_size *= 8;
  213. mutex_unlock(&vm->mm.mutex);
  214. ret = nouveau_gpuobj_new(vm->dev, NULL, pgt_size, 0x1000,
  215. NVOBJ_FLAG_ZERO_ALLOC, &pgt);
  216. mutex_lock(&vm->mm.mutex);
  217. if (unlikely(ret))
  218. return ret;
  219. /* someone beat us to filling the PDE while we didn't have the lock */
  220. if (unlikely(vpgt->refcount[big]++)) {
  221. mutex_unlock(&vm->mm.mutex);
  222. nouveau_gpuobj_ref(NULL, &pgt);
  223. mutex_lock(&vm->mm.mutex);
  224. return 0;
  225. }
  226. vpgt->obj[big] = pgt;
  227. list_for_each_entry(vpgd, &vm->pgd_list, head) {
  228. vm->map_pgt(vpgd->obj, pde, vpgt->obj);
  229. }
  230. return 0;
  231. }
  232. int
  233. nouveau_vm_get(struct nouveau_vm *vm, u64 size, u32 page_shift,
  234. u32 access, struct nouveau_vma *vma)
  235. {
  236. u32 align = (1 << page_shift) >> 12;
  237. u32 msize = size >> 12;
  238. u32 fpde, lpde, pde;
  239. int ret;
  240. mutex_lock(&vm->mm.mutex);
  241. ret = nouveau_mm_get(&vm->mm, page_shift, msize, 0, align, &vma->node);
  242. if (unlikely(ret != 0)) {
  243. mutex_unlock(&vm->mm.mutex);
  244. return ret;
  245. }
  246. fpde = (vma->node->offset >> vm->pgt_bits);
  247. lpde = (vma->node->offset + vma->node->length - 1) >> vm->pgt_bits;
  248. for (pde = fpde; pde <= lpde; pde++) {
  249. struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
  250. int big = (vma->node->type != vm->spg_shift);
  251. if (likely(vpgt->refcount[big])) {
  252. vpgt->refcount[big]++;
  253. continue;
  254. }
  255. ret = nouveau_vm_map_pgt(vm, pde, vma->node->type);
  256. if (ret) {
  257. if (pde != fpde)
  258. nouveau_vm_unmap_pgt(vm, big, fpde, pde - 1);
  259. nouveau_mm_put(&vm->mm, vma->node);
  260. mutex_unlock(&vm->mm.mutex);
  261. vma->node = NULL;
  262. return ret;
  263. }
  264. }
  265. mutex_unlock(&vm->mm.mutex);
  266. vma->vm = vm;
  267. vma->offset = (u64)vma->node->offset << 12;
  268. vma->access = access;
  269. return 0;
  270. }
  271. void
  272. nouveau_vm_put(struct nouveau_vma *vma)
  273. {
  274. struct nouveau_vm *vm = vma->vm;
  275. u32 fpde, lpde;
  276. if (unlikely(vma->node == NULL))
  277. return;
  278. fpde = (vma->node->offset >> vm->pgt_bits);
  279. lpde = (vma->node->offset + vma->node->length - 1) >> vm->pgt_bits;
  280. mutex_lock(&vm->mm.mutex);
  281. nouveau_vm_unmap_pgt(vm, vma->node->type != vm->spg_shift, fpde, lpde);
  282. nouveau_mm_put(&vm->mm, vma->node);
  283. vma->node = NULL;
  284. mutex_unlock(&vm->mm.mutex);
  285. }
  286. int
  287. nouveau_vm_new(struct drm_device *dev, u64 offset, u64 length, u64 mm_offset,
  288. struct nouveau_vm **pvm)
  289. {
  290. struct drm_nouveau_private *dev_priv = dev->dev_private;
  291. struct nouveau_vm *vm;
  292. u64 mm_length = (offset + length) - mm_offset;
  293. u32 block, pgt_bits;
  294. int ret;
  295. vm = kzalloc(sizeof(*vm), GFP_KERNEL);
  296. if (!vm)
  297. return -ENOMEM;
  298. if (dev_priv->card_type == NV_50) {
  299. vm->map_pgt = nv50_vm_map_pgt;
  300. vm->map = nv50_vm_map;
  301. vm->map_sg = nv50_vm_map_sg;
  302. vm->unmap = nv50_vm_unmap;
  303. vm->flush = nv50_vm_flush;
  304. vm->spg_shift = 12;
  305. vm->lpg_shift = 16;
  306. pgt_bits = 29;
  307. block = (1 << pgt_bits);
  308. if (length < block)
  309. block = length;
  310. } else
  311. if (dev_priv->card_type >= NV_C0) {
  312. vm->map_pgt = nvc0_vm_map_pgt;
  313. vm->map = nvc0_vm_map;
  314. vm->map_sg = nvc0_vm_map_sg;
  315. vm->unmap = nvc0_vm_unmap;
  316. vm->flush = nvc0_vm_flush;
  317. vm->spg_shift = 12;
  318. vm->lpg_shift = 17;
  319. pgt_bits = 27;
  320. block = 4096;
  321. } else {
  322. kfree(vm);
  323. return -ENOSYS;
  324. }
  325. vm->fpde = offset >> pgt_bits;
  326. vm->lpde = (offset + length - 1) >> pgt_bits;
  327. vm->pgt = kcalloc(vm->lpde - vm->fpde + 1, sizeof(*vm->pgt), GFP_KERNEL);
  328. if (!vm->pgt) {
  329. kfree(vm);
  330. return -ENOMEM;
  331. }
  332. INIT_LIST_HEAD(&vm->pgd_list);
  333. vm->dev = dev;
  334. vm->refcount = 1;
  335. vm->pgt_bits = pgt_bits - 12;
  336. ret = nouveau_mm_init(&vm->mm, mm_offset >> 12, mm_length >> 12,
  337. block >> 12);
  338. if (ret) {
  339. kfree(vm);
  340. return ret;
  341. }
  342. *pvm = vm;
  343. return 0;
  344. }
  345. static int
  346. nouveau_vm_link(struct nouveau_vm *vm, struct nouveau_gpuobj *pgd)
  347. {
  348. struct nouveau_vm_pgd *vpgd;
  349. int i;
  350. if (!pgd)
  351. return 0;
  352. vpgd = kzalloc(sizeof(*vpgd), GFP_KERNEL);
  353. if (!vpgd)
  354. return -ENOMEM;
  355. nouveau_gpuobj_ref(pgd, &vpgd->obj);
  356. mutex_lock(&vm->mm.mutex);
  357. for (i = vm->fpde; i <= vm->lpde; i++)
  358. vm->map_pgt(pgd, i, vm->pgt[i - vm->fpde].obj);
  359. list_add(&vpgd->head, &vm->pgd_list);
  360. mutex_unlock(&vm->mm.mutex);
  361. return 0;
  362. }
  363. static void
  364. nouveau_vm_unlink(struct nouveau_vm *vm, struct nouveau_gpuobj *mpgd)
  365. {
  366. struct nouveau_vm_pgd *vpgd, *tmp;
  367. struct nouveau_gpuobj *pgd = NULL;
  368. if (!mpgd)
  369. return;
  370. mutex_lock(&vm->mm.mutex);
  371. list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
  372. if (vpgd->obj == mpgd) {
  373. pgd = vpgd->obj;
  374. list_del(&vpgd->head);
  375. kfree(vpgd);
  376. break;
  377. }
  378. }
  379. mutex_unlock(&vm->mm.mutex);
  380. nouveau_gpuobj_ref(NULL, &pgd);
  381. }
  382. static void
  383. nouveau_vm_del(struct nouveau_vm *vm)
  384. {
  385. struct nouveau_vm_pgd *vpgd, *tmp;
  386. list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
  387. nouveau_vm_unlink(vm, vpgd->obj);
  388. }
  389. nouveau_mm_fini(&vm->mm);
  390. kfree(vm->pgt);
  391. kfree(vm);
  392. }
  393. int
  394. nouveau_vm_ref(struct nouveau_vm *ref, struct nouveau_vm **ptr,
  395. struct nouveau_gpuobj *pgd)
  396. {
  397. struct nouveau_vm *vm;
  398. int ret;
  399. vm = ref;
  400. if (vm) {
  401. ret = nouveau_vm_link(vm, pgd);
  402. if (ret)
  403. return ret;
  404. vm->refcount++;
  405. }
  406. vm = *ptr;
  407. *ptr = ref;
  408. if (vm) {
  409. nouveau_vm_unlink(vm, pgd);
  410. if (--vm->refcount == 0)
  411. nouveau_vm_del(vm);
  412. }
  413. return 0;
  414. }