nouveau_vm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. pde++;
  55. pte = 0;
  56. }
  57. delta += (u64)len << vma->node->type;
  58. }
  59. }
  60. vm->flush(vm);
  61. }
  62. void
  63. nouveau_vm_map(struct nouveau_vma *vma, struct nouveau_mem *node)
  64. {
  65. nouveau_vm_map_at(vma, 0, node);
  66. }
  67. void
  68. nouveau_vm_map_sg(struct nouveau_vma *vma, u64 delta, u64 length,
  69. struct nouveau_mem *mem, dma_addr_t *list)
  70. {
  71. struct nouveau_vm *vm = vma->vm;
  72. int big = vma->node->type != vm->spg_shift;
  73. u32 offset = vma->node->offset + (delta >> 12);
  74. u32 bits = vma->node->type - 12;
  75. u32 num = length >> vma->node->type;
  76. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  77. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  78. u32 max = 1 << (vm->pgt_bits - bits);
  79. u32 end, len;
  80. while (num) {
  81. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  82. end = (pte + num);
  83. if (unlikely(end >= max))
  84. end = max;
  85. len = end - pte;
  86. vm->map_sg(vma, pgt, mem, pte, len, list);
  87. num -= len;
  88. pte += len;
  89. list += len;
  90. if (unlikely(end >= max)) {
  91. pde++;
  92. pte = 0;
  93. }
  94. }
  95. vm->flush(vm);
  96. }
  97. void
  98. nouveau_vm_unmap_at(struct nouveau_vma *vma, u64 delta, u64 length)
  99. {
  100. struct nouveau_vm *vm = vma->vm;
  101. int big = vma->node->type != vm->spg_shift;
  102. u32 offset = vma->node->offset + (delta >> 12);
  103. u32 bits = vma->node->type - 12;
  104. u32 num = length >> vma->node->type;
  105. u32 pde = (offset >> vm->pgt_bits) - vm->fpde;
  106. u32 pte = (offset & ((1 << vm->pgt_bits) - 1)) >> bits;
  107. u32 max = 1 << (vm->pgt_bits - bits);
  108. u32 end, len;
  109. while (num) {
  110. struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
  111. end = (pte + num);
  112. if (unlikely(end >= max))
  113. end = max;
  114. len = end - pte;
  115. vm->unmap(pgt, pte, len);
  116. num -= len;
  117. pte += len;
  118. if (unlikely(end >= max)) {
  119. pde++;
  120. pte = 0;
  121. }
  122. }
  123. vm->flush(vm);
  124. }
  125. void
  126. nouveau_vm_unmap(struct nouveau_vma *vma)
  127. {
  128. nouveau_vm_unmap_at(vma, 0, (u64)vma->node->length << 12);
  129. }
  130. static void
  131. nouveau_vm_unmap_pgt(struct nouveau_vm *vm, int big, u32 fpde, u32 lpde)
  132. {
  133. struct nouveau_vm_pgd *vpgd;
  134. struct nouveau_vm_pgt *vpgt;
  135. struct nouveau_gpuobj *pgt;
  136. u32 pde;
  137. for (pde = fpde; pde <= lpde; pde++) {
  138. vpgt = &vm->pgt[pde - vm->fpde];
  139. if (--vpgt->refcount[big])
  140. continue;
  141. pgt = vpgt->obj[big];
  142. vpgt->obj[big] = NULL;
  143. list_for_each_entry(vpgd, &vm->pgd_list, head) {
  144. vm->map_pgt(vpgd->obj, pde, vpgt->obj);
  145. }
  146. mutex_unlock(&vm->mm->mutex);
  147. nouveau_gpuobj_ref(NULL, &pgt);
  148. mutex_lock(&vm->mm->mutex);
  149. }
  150. }
  151. static int
  152. nouveau_vm_map_pgt(struct nouveau_vm *vm, u32 pde, u32 type)
  153. {
  154. struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
  155. struct nouveau_vm_pgd *vpgd;
  156. struct nouveau_gpuobj *pgt;
  157. int big = (type != vm->spg_shift);
  158. u32 pgt_size;
  159. int ret;
  160. pgt_size = (1 << (vm->pgt_bits + 12)) >> type;
  161. pgt_size *= 8;
  162. mutex_unlock(&vm->mm->mutex);
  163. ret = nouveau_gpuobj_new(vm->dev, NULL, pgt_size, 0x1000,
  164. NVOBJ_FLAG_ZERO_ALLOC, &pgt);
  165. mutex_lock(&vm->mm->mutex);
  166. if (unlikely(ret))
  167. return ret;
  168. /* someone beat us to filling the PDE while we didn't have the lock */
  169. if (unlikely(vpgt->refcount[big]++)) {
  170. mutex_unlock(&vm->mm->mutex);
  171. nouveau_gpuobj_ref(NULL, &pgt);
  172. mutex_lock(&vm->mm->mutex);
  173. return 0;
  174. }
  175. vpgt->obj[big] = pgt;
  176. list_for_each_entry(vpgd, &vm->pgd_list, head) {
  177. vm->map_pgt(vpgd->obj, pde, vpgt->obj);
  178. }
  179. return 0;
  180. }
  181. int
  182. nouveau_vm_get(struct nouveau_vm *vm, u64 size, u32 page_shift,
  183. u32 access, struct nouveau_vma *vma)
  184. {
  185. u32 align = (1 << page_shift) >> 12;
  186. u32 msize = size >> 12;
  187. u32 fpde, lpde, pde;
  188. int ret;
  189. mutex_lock(&vm->mm->mutex);
  190. ret = nouveau_mm_get(vm->mm, page_shift, msize, 0, align, &vma->node);
  191. if (unlikely(ret != 0)) {
  192. mutex_unlock(&vm->mm->mutex);
  193. return ret;
  194. }
  195. fpde = (vma->node->offset >> vm->pgt_bits);
  196. lpde = (vma->node->offset + vma->node->length - 1) >> vm->pgt_bits;
  197. for (pde = fpde; pde <= lpde; pde++) {
  198. struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
  199. int big = (vma->node->type != vm->spg_shift);
  200. if (likely(vpgt->refcount[big])) {
  201. vpgt->refcount[big]++;
  202. continue;
  203. }
  204. ret = nouveau_vm_map_pgt(vm, pde, vma->node->type);
  205. if (ret) {
  206. if (pde != fpde)
  207. nouveau_vm_unmap_pgt(vm, big, fpde, pde - 1);
  208. nouveau_mm_put(vm->mm, vma->node);
  209. mutex_unlock(&vm->mm->mutex);
  210. vma->node = NULL;
  211. return ret;
  212. }
  213. }
  214. mutex_unlock(&vm->mm->mutex);
  215. vma->vm = vm;
  216. vma->offset = (u64)vma->node->offset << 12;
  217. vma->access = access;
  218. return 0;
  219. }
  220. void
  221. nouveau_vm_put(struct nouveau_vma *vma)
  222. {
  223. struct nouveau_vm *vm = vma->vm;
  224. u32 fpde, lpde;
  225. if (unlikely(vma->node == NULL))
  226. return;
  227. fpde = (vma->node->offset >> vm->pgt_bits);
  228. lpde = (vma->node->offset + vma->node->length - 1) >> vm->pgt_bits;
  229. mutex_lock(&vm->mm->mutex);
  230. nouveau_vm_unmap_pgt(vm, vma->node->type != vm->spg_shift, fpde, lpde);
  231. nouveau_mm_put(vm->mm, vma->node);
  232. vma->node = NULL;
  233. mutex_unlock(&vm->mm->mutex);
  234. }
  235. int
  236. nouveau_vm_new(struct drm_device *dev, u64 offset, u64 length, u64 mm_offset,
  237. struct nouveau_vm **pvm)
  238. {
  239. struct drm_nouveau_private *dev_priv = dev->dev_private;
  240. struct nouveau_vm *vm;
  241. u64 mm_length = (offset + length) - mm_offset;
  242. u32 block, pgt_bits;
  243. int ret;
  244. vm = kzalloc(sizeof(*vm), GFP_KERNEL);
  245. if (!vm)
  246. return -ENOMEM;
  247. if (dev_priv->card_type == NV_50) {
  248. vm->map_pgt = nv50_vm_map_pgt;
  249. vm->map = nv50_vm_map;
  250. vm->map_sg = nv50_vm_map_sg;
  251. vm->unmap = nv50_vm_unmap;
  252. vm->flush = nv50_vm_flush;
  253. vm->spg_shift = 12;
  254. vm->lpg_shift = 16;
  255. pgt_bits = 29;
  256. block = (1 << pgt_bits);
  257. if (length < block)
  258. block = length;
  259. } else
  260. if (dev_priv->card_type == NV_C0) {
  261. vm->map_pgt = nvc0_vm_map_pgt;
  262. vm->map = nvc0_vm_map;
  263. vm->map_sg = nvc0_vm_map_sg;
  264. vm->unmap = nvc0_vm_unmap;
  265. vm->flush = nvc0_vm_flush;
  266. vm->spg_shift = 12;
  267. vm->lpg_shift = 17;
  268. pgt_bits = 27;
  269. block = 4096;
  270. } else {
  271. kfree(vm);
  272. return -ENOSYS;
  273. }
  274. vm->fpde = offset >> pgt_bits;
  275. vm->lpde = (offset + length - 1) >> pgt_bits;
  276. vm->pgt = kcalloc(vm->lpde - vm->fpde + 1, sizeof(*vm->pgt), GFP_KERNEL);
  277. if (!vm->pgt) {
  278. kfree(vm);
  279. return -ENOMEM;
  280. }
  281. INIT_LIST_HEAD(&vm->pgd_list);
  282. vm->dev = dev;
  283. vm->refcount = 1;
  284. vm->pgt_bits = pgt_bits - 12;
  285. ret = nouveau_mm_init(&vm->mm, mm_offset >> 12, mm_length >> 12,
  286. block >> 12);
  287. if (ret) {
  288. kfree(vm);
  289. return ret;
  290. }
  291. *pvm = vm;
  292. return 0;
  293. }
  294. static int
  295. nouveau_vm_link(struct nouveau_vm *vm, struct nouveau_gpuobj *pgd)
  296. {
  297. struct nouveau_vm_pgd *vpgd;
  298. int i;
  299. if (!pgd)
  300. return 0;
  301. vpgd = kzalloc(sizeof(*vpgd), GFP_KERNEL);
  302. if (!vpgd)
  303. return -ENOMEM;
  304. nouveau_gpuobj_ref(pgd, &vpgd->obj);
  305. mutex_lock(&vm->mm->mutex);
  306. for (i = vm->fpde; i <= vm->lpde; i++)
  307. vm->map_pgt(pgd, i, vm->pgt[i - vm->fpde].obj);
  308. list_add(&vpgd->head, &vm->pgd_list);
  309. mutex_unlock(&vm->mm->mutex);
  310. return 0;
  311. }
  312. static void
  313. nouveau_vm_unlink(struct nouveau_vm *vm, struct nouveau_gpuobj *pgd)
  314. {
  315. struct nouveau_vm_pgd *vpgd, *tmp;
  316. if (!pgd)
  317. return;
  318. mutex_lock(&vm->mm->mutex);
  319. list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
  320. if (vpgd->obj != pgd)
  321. continue;
  322. list_del(&vpgd->head);
  323. nouveau_gpuobj_ref(NULL, &vpgd->obj);
  324. kfree(vpgd);
  325. }
  326. mutex_unlock(&vm->mm->mutex);
  327. }
  328. static void
  329. nouveau_vm_del(struct nouveau_vm *vm)
  330. {
  331. struct nouveau_vm_pgd *vpgd, *tmp;
  332. list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
  333. nouveau_vm_unlink(vm, vpgd->obj);
  334. }
  335. WARN_ON(nouveau_mm_fini(&vm->mm) != 0);
  336. kfree(vm->pgt);
  337. kfree(vm);
  338. }
  339. int
  340. nouveau_vm_ref(struct nouveau_vm *ref, struct nouveau_vm **ptr,
  341. struct nouveau_gpuobj *pgd)
  342. {
  343. struct nouveau_vm *vm;
  344. int ret;
  345. vm = ref;
  346. if (vm) {
  347. ret = nouveau_vm_link(vm, pgd);
  348. if (ret)
  349. return ret;
  350. vm->refcount++;
  351. }
  352. vm = *ptr;
  353. *ptr = ref;
  354. if (vm) {
  355. nouveau_vm_unlink(vm, pgd);
  356. if (--vm->refcount == 0)
  357. nouveau_vm_del(vm);
  358. }
  359. return 0;
  360. }