nouveau_svm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /*
  2. * Copyright 2018 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. #include "nouveau_svm.h"
  23. #include "nouveau_drv.h"
  24. #include "nouveau_chan.h"
  25. #include "nouveau_dmem.h"
  26. #include <nvif/notify.h>
  27. #include <nvif/object.h>
  28. #include <nvif/vmm.h>
  29. #include <nvif/class.h>
  30. #include <nvif/clb069.h>
  31. #include <nvif/ifc00d.h>
  32. #include <linux/sched/mm.h>
  33. #include <linux/sort.h>
  34. #include <linux/hmm.h>
  35. #include <linux/memremap.h>
  36. #include <linux/rmap.h>
  37. struct nouveau_svm {
  38. struct nouveau_drm *drm;
  39. struct mutex mutex;
  40. struct list_head inst;
  41. struct nouveau_svm_fault_buffer {
  42. int id;
  43. struct nvif_object object;
  44. u32 entries;
  45. u32 getaddr;
  46. u32 putaddr;
  47. u32 get;
  48. u32 put;
  49. struct nvif_notify notify;
  50. struct nouveau_svm_fault {
  51. u64 inst;
  52. u64 addr;
  53. u64 time;
  54. u32 engine;
  55. u8 gpc;
  56. u8 hub;
  57. u8 access;
  58. u8 client;
  59. u8 fault;
  60. struct nouveau_svmm *svmm;
  61. } **fault;
  62. int fault_nr;
  63. } buffer[1];
  64. };
  65. #define FAULT_ACCESS_READ 0
  66. #define FAULT_ACCESS_WRITE 1
  67. #define FAULT_ACCESS_ATOMIC 2
  68. #define FAULT_ACCESS_PREFETCH 3
  69. #define SVM_DBG(s,f,a...) NV_DEBUG((s)->drm, "svm: "f"\n", ##a)
  70. #define SVM_ERR(s,f,a...) NV_WARN((s)->drm, "svm: "f"\n", ##a)
  71. struct nouveau_pfnmap_args {
  72. struct nvif_ioctl_v0 i;
  73. struct nvif_ioctl_mthd_v0 m;
  74. struct nvif_vmm_pfnmap_v0 p;
  75. };
  76. struct nouveau_ivmm {
  77. struct nouveau_svmm *svmm;
  78. u64 inst;
  79. struct list_head head;
  80. };
  81. static struct nouveau_ivmm *
  82. nouveau_ivmm_find(struct nouveau_svm *svm, u64 inst)
  83. {
  84. struct nouveau_ivmm *ivmm;
  85. list_for_each_entry(ivmm, &svm->inst, head) {
  86. if (ivmm->inst == inst)
  87. return ivmm;
  88. }
  89. return NULL;
  90. }
  91. #define SVMM_DBG(s,f,a...) \
  92. NV_DEBUG((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
  93. #define SVMM_ERR(s,f,a...) \
  94. NV_WARN((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
  95. int
  96. nouveau_svmm_bind(struct drm_device *dev, void *data,
  97. struct drm_file *file_priv)
  98. {
  99. struct nouveau_cli *cli = nouveau_cli(file_priv);
  100. struct drm_nouveau_svm_bind *args = data;
  101. unsigned target, cmd, priority;
  102. unsigned long addr, end;
  103. struct mm_struct *mm;
  104. args->va_start &= PAGE_MASK;
  105. args->va_end = ALIGN(args->va_end, PAGE_SIZE);
  106. /* Sanity check arguments */
  107. if (args->reserved0 || args->reserved1)
  108. return -EINVAL;
  109. if (args->header & (~NOUVEAU_SVM_BIND_VALID_MASK))
  110. return -EINVAL;
  111. if (args->va_start >= args->va_end)
  112. return -EINVAL;
  113. cmd = args->header >> NOUVEAU_SVM_BIND_COMMAND_SHIFT;
  114. cmd &= NOUVEAU_SVM_BIND_COMMAND_MASK;
  115. switch (cmd) {
  116. case NOUVEAU_SVM_BIND_COMMAND__MIGRATE:
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. priority = args->header >> NOUVEAU_SVM_BIND_PRIORITY_SHIFT;
  122. priority &= NOUVEAU_SVM_BIND_PRIORITY_MASK;
  123. /* FIXME support CPU target ie all target value < GPU_VRAM */
  124. target = args->header >> NOUVEAU_SVM_BIND_TARGET_SHIFT;
  125. target &= NOUVEAU_SVM_BIND_TARGET_MASK;
  126. switch (target) {
  127. case NOUVEAU_SVM_BIND_TARGET__GPU_VRAM:
  128. break;
  129. default:
  130. return -EINVAL;
  131. }
  132. /*
  133. * FIXME: For now refuse non 0 stride, we need to change the migrate
  134. * kernel function to handle stride to avoid to create a mess within
  135. * each device driver.
  136. */
  137. if (args->stride)
  138. return -EINVAL;
  139. /*
  140. * Ok we are ask to do something sane, for now we only support migrate
  141. * commands but we will add things like memory policy (what to do on
  142. * page fault) and maybe some other commands.
  143. */
  144. mm = get_task_mm(current);
  145. if (!mm) {
  146. return -EINVAL;
  147. }
  148. mmap_read_lock(mm);
  149. if (!cli->svm.svmm) {
  150. mmap_read_unlock(mm);
  151. mmput(mm);
  152. return -EINVAL;
  153. }
  154. for (addr = args->va_start, end = args->va_end; addr < end;) {
  155. struct vm_area_struct *vma;
  156. unsigned long next;
  157. vma = find_vma_intersection(mm, addr, end);
  158. if (!vma)
  159. break;
  160. addr = max(addr, vma->vm_start);
  161. next = min(vma->vm_end, end);
  162. /* This is a best effort so we ignore errors */
  163. nouveau_dmem_migrate_vma(cli->drm, cli->svm.svmm, vma, addr,
  164. next);
  165. addr = next;
  166. }
  167. /*
  168. * FIXME Return the number of page we have migrated, again we need to
  169. * update the migrate API to return that information so that we can
  170. * report it to user space.
  171. */
  172. args->result = 0;
  173. mmap_read_unlock(mm);
  174. mmput(mm);
  175. return 0;
  176. }
  177. /* Unlink channel instance from SVMM. */
  178. void
  179. nouveau_svmm_part(struct nouveau_svmm *svmm, u64 inst)
  180. {
  181. struct nouveau_ivmm *ivmm;
  182. if (svmm) {
  183. mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
  184. ivmm = nouveau_ivmm_find(svmm->vmm->cli->drm->svm, inst);
  185. if (ivmm) {
  186. list_del(&ivmm->head);
  187. kfree(ivmm);
  188. }
  189. mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
  190. }
  191. }
  192. /* Link channel instance to SVMM. */
  193. int
  194. nouveau_svmm_join(struct nouveau_svmm *svmm, u64 inst)
  195. {
  196. struct nouveau_ivmm *ivmm;
  197. if (svmm) {
  198. if (!(ivmm = kmalloc(sizeof(*ivmm), GFP_KERNEL)))
  199. return -ENOMEM;
  200. ivmm->svmm = svmm;
  201. ivmm->inst = inst;
  202. mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
  203. list_add(&ivmm->head, &svmm->vmm->cli->drm->svm->inst);
  204. mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
  205. }
  206. return 0;
  207. }
  208. /* Invalidate SVMM address-range on GPU. */
  209. void
  210. nouveau_svmm_invalidate(struct nouveau_svmm *svmm, u64 start, u64 limit)
  211. {
  212. if (limit > start) {
  213. nvif_object_mthd(&svmm->vmm->vmm.object, NVIF_VMM_V0_PFNCLR,
  214. &(struct nvif_vmm_pfnclr_v0) {
  215. .addr = start,
  216. .size = limit - start,
  217. }, sizeof(struct nvif_vmm_pfnclr_v0));
  218. }
  219. }
  220. static int
  221. nouveau_svmm_invalidate_range_start(struct mmu_notifier *mn,
  222. const struct mmu_notifier_range *update)
  223. {
  224. struct nouveau_svmm *svmm =
  225. container_of(mn, struct nouveau_svmm, notifier);
  226. unsigned long start = update->start;
  227. unsigned long limit = update->end;
  228. if (!mmu_notifier_range_blockable(update))
  229. return -EAGAIN;
  230. SVMM_DBG(svmm, "invalidate %016lx-%016lx", start, limit);
  231. mutex_lock(&svmm->mutex);
  232. if (unlikely(!svmm->vmm))
  233. goto out;
  234. /*
  235. * Ignore invalidation callbacks for device private pages since
  236. * the invalidation is handled as part of the migration process.
  237. */
  238. if (update->event == MMU_NOTIFY_MIGRATE &&
  239. update->owner == svmm->vmm->cli->drm->dev)
  240. goto out;
  241. if (limit > svmm->unmanaged.start && start < svmm->unmanaged.limit) {
  242. if (start < svmm->unmanaged.start) {
  243. nouveau_svmm_invalidate(svmm, start,
  244. svmm->unmanaged.limit);
  245. }
  246. start = svmm->unmanaged.limit;
  247. }
  248. nouveau_svmm_invalidate(svmm, start, limit);
  249. out:
  250. mutex_unlock(&svmm->mutex);
  251. return 0;
  252. }
  253. static void nouveau_svmm_free_notifier(struct mmu_notifier *mn)
  254. {
  255. kfree(container_of(mn, struct nouveau_svmm, notifier));
  256. }
  257. static const struct mmu_notifier_ops nouveau_mn_ops = {
  258. .invalidate_range_start = nouveau_svmm_invalidate_range_start,
  259. .free_notifier = nouveau_svmm_free_notifier,
  260. };
  261. void
  262. nouveau_svmm_fini(struct nouveau_svmm **psvmm)
  263. {
  264. struct nouveau_svmm *svmm = *psvmm;
  265. if (svmm) {
  266. mutex_lock(&svmm->mutex);
  267. svmm->vmm = NULL;
  268. mutex_unlock(&svmm->mutex);
  269. mmu_notifier_put(&svmm->notifier);
  270. *psvmm = NULL;
  271. }
  272. }
  273. int
  274. nouveau_svmm_init(struct drm_device *dev, void *data,
  275. struct drm_file *file_priv)
  276. {
  277. struct nouveau_cli *cli = nouveau_cli(file_priv);
  278. struct nouveau_svmm *svmm;
  279. struct drm_nouveau_svm_init *args = data;
  280. int ret;
  281. /* We need to fail if svm is disabled */
  282. if (!cli->drm->svm)
  283. return -ENOSYS;
  284. /* Allocate tracking for SVM-enabled VMM. */
  285. if (!(svmm = kzalloc(sizeof(*svmm), GFP_KERNEL)))
  286. return -ENOMEM;
  287. svmm->vmm = &cli->svm;
  288. svmm->unmanaged.start = args->unmanaged_addr;
  289. svmm->unmanaged.limit = args->unmanaged_addr + args->unmanaged_size;
  290. mutex_init(&svmm->mutex);
  291. /* Check that SVM isn't already enabled for the client. */
  292. mutex_lock(&cli->mutex);
  293. if (cli->svm.cli) {
  294. ret = -EBUSY;
  295. goto out_free;
  296. }
  297. /* Allocate a new GPU VMM that can support SVM (managed by the
  298. * client, with replayable faults enabled).
  299. *
  300. * All future channel/memory allocations will make use of this
  301. * VMM instead of the standard one.
  302. */
  303. ret = nvif_vmm_ctor(&cli->mmu, "svmVmm",
  304. cli->vmm.vmm.object.oclass, true,
  305. args->unmanaged_addr, args->unmanaged_size,
  306. &(struct gp100_vmm_v0) {
  307. .fault_replay = true,
  308. }, sizeof(struct gp100_vmm_v0), &cli->svm.vmm);
  309. if (ret)
  310. goto out_free;
  311. mmap_write_lock(current->mm);
  312. svmm->notifier.ops = &nouveau_mn_ops;
  313. ret = __mmu_notifier_register(&svmm->notifier, current->mm);
  314. if (ret)
  315. goto out_mm_unlock;
  316. /* Note, ownership of svmm transfers to mmu_notifier */
  317. cli->svm.svmm = svmm;
  318. cli->svm.cli = cli;
  319. mmap_write_unlock(current->mm);
  320. mutex_unlock(&cli->mutex);
  321. return 0;
  322. out_mm_unlock:
  323. mmap_write_unlock(current->mm);
  324. out_free:
  325. mutex_unlock(&cli->mutex);
  326. kfree(svmm);
  327. return ret;
  328. }
  329. /* Issue fault replay for GPU to retry accesses that faulted previously. */
  330. static void
  331. nouveau_svm_fault_replay(struct nouveau_svm *svm)
  332. {
  333. SVM_DBG(svm, "replay");
  334. WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
  335. GP100_VMM_VN_FAULT_REPLAY,
  336. &(struct gp100_vmm_fault_replay_vn) {},
  337. sizeof(struct gp100_vmm_fault_replay_vn)));
  338. }
  339. /* Cancel a replayable fault that could not be handled.
  340. *
  341. * Cancelling the fault will trigger recovery to reset the engine
  342. * and kill the offending channel (ie. GPU SIGSEGV).
  343. */
  344. static void
  345. nouveau_svm_fault_cancel(struct nouveau_svm *svm,
  346. u64 inst, u8 hub, u8 gpc, u8 client)
  347. {
  348. SVM_DBG(svm, "cancel %016llx %d %02x %02x", inst, hub, gpc, client);
  349. WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
  350. GP100_VMM_VN_FAULT_CANCEL,
  351. &(struct gp100_vmm_fault_cancel_v0) {
  352. .hub = hub,
  353. .gpc = gpc,
  354. .client = client,
  355. .inst = inst,
  356. }, sizeof(struct gp100_vmm_fault_cancel_v0)));
  357. }
  358. static void
  359. nouveau_svm_fault_cancel_fault(struct nouveau_svm *svm,
  360. struct nouveau_svm_fault *fault)
  361. {
  362. nouveau_svm_fault_cancel(svm, fault->inst,
  363. fault->hub,
  364. fault->gpc,
  365. fault->client);
  366. }
  367. static int
  368. nouveau_svm_fault_priority(u8 fault)
  369. {
  370. switch (fault) {
  371. case FAULT_ACCESS_PREFETCH:
  372. return 0;
  373. case FAULT_ACCESS_READ:
  374. return 1;
  375. case FAULT_ACCESS_WRITE:
  376. return 2;
  377. case FAULT_ACCESS_ATOMIC:
  378. return 3;
  379. default:
  380. WARN_ON_ONCE(1);
  381. return -1;
  382. }
  383. }
  384. static int
  385. nouveau_svm_fault_cmp(const void *a, const void *b)
  386. {
  387. const struct nouveau_svm_fault *fa = *(struct nouveau_svm_fault **)a;
  388. const struct nouveau_svm_fault *fb = *(struct nouveau_svm_fault **)b;
  389. int ret;
  390. if ((ret = (s64)fa->inst - fb->inst))
  391. return ret;
  392. if ((ret = (s64)fa->addr - fb->addr))
  393. return ret;
  394. return nouveau_svm_fault_priority(fa->access) -
  395. nouveau_svm_fault_priority(fb->access);
  396. }
  397. static void
  398. nouveau_svm_fault_cache(struct nouveau_svm *svm,
  399. struct nouveau_svm_fault_buffer *buffer, u32 offset)
  400. {
  401. struct nvif_object *memory = &buffer->object;
  402. const u32 instlo = nvif_rd32(memory, offset + 0x00);
  403. const u32 insthi = nvif_rd32(memory, offset + 0x04);
  404. const u32 addrlo = nvif_rd32(memory, offset + 0x08);
  405. const u32 addrhi = nvif_rd32(memory, offset + 0x0c);
  406. const u32 timelo = nvif_rd32(memory, offset + 0x10);
  407. const u32 timehi = nvif_rd32(memory, offset + 0x14);
  408. const u32 engine = nvif_rd32(memory, offset + 0x18);
  409. const u32 info = nvif_rd32(memory, offset + 0x1c);
  410. const u64 inst = (u64)insthi << 32 | instlo;
  411. const u8 gpc = (info & 0x1f000000) >> 24;
  412. const u8 hub = (info & 0x00100000) >> 20;
  413. const u8 client = (info & 0x00007f00) >> 8;
  414. struct nouveau_svm_fault *fault;
  415. //XXX: i think we're supposed to spin waiting */
  416. if (WARN_ON(!(info & 0x80000000)))
  417. return;
  418. nvif_mask(memory, offset + 0x1c, 0x80000000, 0x00000000);
  419. if (!buffer->fault[buffer->fault_nr]) {
  420. fault = kmalloc(sizeof(*fault), GFP_KERNEL);
  421. if (WARN_ON(!fault)) {
  422. nouveau_svm_fault_cancel(svm, inst, hub, gpc, client);
  423. return;
  424. }
  425. buffer->fault[buffer->fault_nr] = fault;
  426. }
  427. fault = buffer->fault[buffer->fault_nr++];
  428. fault->inst = inst;
  429. fault->addr = (u64)addrhi << 32 | addrlo;
  430. fault->time = (u64)timehi << 32 | timelo;
  431. fault->engine = engine;
  432. fault->gpc = gpc;
  433. fault->hub = hub;
  434. fault->access = (info & 0x000f0000) >> 16;
  435. fault->client = client;
  436. fault->fault = (info & 0x0000001f);
  437. SVM_DBG(svm, "fault %016llx %016llx %02x",
  438. fault->inst, fault->addr, fault->access);
  439. }
  440. struct svm_notifier {
  441. struct mmu_interval_notifier notifier;
  442. struct nouveau_svmm *svmm;
  443. };
  444. static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni,
  445. const struct mmu_notifier_range *range,
  446. unsigned long cur_seq)
  447. {
  448. struct svm_notifier *sn =
  449. container_of(mni, struct svm_notifier, notifier);
  450. if (range->event == MMU_NOTIFY_EXCLUSIVE &&
  451. range->owner == sn->svmm->vmm->cli->drm->dev)
  452. return true;
  453. /*
  454. * serializes the update to mni->invalidate_seq done by caller and
  455. * prevents invalidation of the PTE from progressing while HW is being
  456. * programmed. This is very hacky and only works because the normal
  457. * notifier that does invalidation is always called after the range
  458. * notifier.
  459. */
  460. if (mmu_notifier_range_blockable(range))
  461. mutex_lock(&sn->svmm->mutex);
  462. else if (!mutex_trylock(&sn->svmm->mutex))
  463. return false;
  464. mmu_interval_set_seq(mni, cur_seq);
  465. mutex_unlock(&sn->svmm->mutex);
  466. return true;
  467. }
  468. static const struct mmu_interval_notifier_ops nouveau_svm_mni_ops = {
  469. .invalidate = nouveau_svm_range_invalidate,
  470. };
  471. static void nouveau_hmm_convert_pfn(struct nouveau_drm *drm,
  472. struct hmm_range *range,
  473. struct nouveau_pfnmap_args *args)
  474. {
  475. struct page *page;
  476. /*
  477. * The address prepared here is passed through nvif_object_ioctl()
  478. * to an eventual DMA map in something like gp100_vmm_pgt_pfn()
  479. *
  480. * This is all just encoding the internal hmm representation into a
  481. * different nouveau internal representation.
  482. */
  483. if (!(range->hmm_pfns[0] & HMM_PFN_VALID)) {
  484. args->p.phys[0] = 0;
  485. return;
  486. }
  487. page = hmm_pfn_to_page(range->hmm_pfns[0]);
  488. /*
  489. * Only map compound pages to the GPU if the CPU is also mapping the
  490. * page as a compound page. Otherwise, the PTE protections might not be
  491. * consistent (e.g., CPU only maps part of a compound page).
  492. * Note that the underlying page might still be larger than the
  493. * CPU mapping (e.g., a PUD sized compound page partially mapped with
  494. * a PMD sized page table entry).
  495. */
  496. if (hmm_pfn_to_map_order(range->hmm_pfns[0])) {
  497. unsigned long addr = args->p.addr;
  498. args->p.page = hmm_pfn_to_map_order(range->hmm_pfns[0]) +
  499. PAGE_SHIFT;
  500. args->p.size = 1UL << args->p.page;
  501. args->p.addr &= ~(args->p.size - 1);
  502. page -= (addr - args->p.addr) >> PAGE_SHIFT;
  503. }
  504. if (is_device_private_page(page))
  505. args->p.phys[0] = nouveau_dmem_page_addr(page) |
  506. NVIF_VMM_PFNMAP_V0_V |
  507. NVIF_VMM_PFNMAP_V0_VRAM;
  508. else
  509. args->p.phys[0] = page_to_phys(page) |
  510. NVIF_VMM_PFNMAP_V0_V |
  511. NVIF_VMM_PFNMAP_V0_HOST;
  512. if (range->hmm_pfns[0] & HMM_PFN_WRITE)
  513. args->p.phys[0] |= NVIF_VMM_PFNMAP_V0_W;
  514. }
  515. static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
  516. struct nouveau_drm *drm,
  517. struct nouveau_pfnmap_args *args, u32 size,
  518. struct svm_notifier *notifier)
  519. {
  520. unsigned long timeout =
  521. jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
  522. struct mm_struct *mm = svmm->notifier.mm;
  523. struct page *page;
  524. unsigned long start = args->p.addr;
  525. unsigned long notifier_seq;
  526. int ret = 0;
  527. ret = mmu_interval_notifier_insert(&notifier->notifier, mm,
  528. args->p.addr, args->p.size,
  529. &nouveau_svm_mni_ops);
  530. if (ret)
  531. return ret;
  532. while (true) {
  533. if (time_after(jiffies, timeout)) {
  534. ret = -EBUSY;
  535. goto out;
  536. }
  537. notifier_seq = mmu_interval_read_begin(&notifier->notifier);
  538. mmap_read_lock(mm);
  539. ret = make_device_exclusive_range(mm, start, start + PAGE_SIZE,
  540. &page, drm->dev);
  541. mmap_read_unlock(mm);
  542. if (ret <= 0 || !page) {
  543. ret = -EINVAL;
  544. goto out;
  545. }
  546. mutex_lock(&svmm->mutex);
  547. if (!mmu_interval_read_retry(&notifier->notifier,
  548. notifier_seq))
  549. break;
  550. mutex_unlock(&svmm->mutex);
  551. }
  552. /* Map the page on the GPU. */
  553. args->p.page = 12;
  554. args->p.size = PAGE_SIZE;
  555. args->p.addr = start;
  556. args->p.phys[0] = page_to_phys(page) |
  557. NVIF_VMM_PFNMAP_V0_V |
  558. NVIF_VMM_PFNMAP_V0_W |
  559. NVIF_VMM_PFNMAP_V0_A |
  560. NVIF_VMM_PFNMAP_V0_HOST;
  561. ret = nvif_object_ioctl(&svmm->vmm->vmm.object, args, size, NULL);
  562. mutex_unlock(&svmm->mutex);
  563. unlock_page(page);
  564. put_page(page);
  565. out:
  566. mmu_interval_notifier_remove(&notifier->notifier);
  567. return ret;
  568. }
  569. static int nouveau_range_fault(struct nouveau_svmm *svmm,
  570. struct nouveau_drm *drm,
  571. struct nouveau_pfnmap_args *args, u32 size,
  572. unsigned long hmm_flags,
  573. struct svm_notifier *notifier)
  574. {
  575. unsigned long timeout =
  576. jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
  577. /* Have HMM fault pages within the fault window to the GPU. */
  578. unsigned long hmm_pfns[1];
  579. struct hmm_range range = {
  580. .notifier = &notifier->notifier,
  581. .default_flags = hmm_flags,
  582. .hmm_pfns = hmm_pfns,
  583. .dev_private_owner = drm->dev,
  584. };
  585. struct mm_struct *mm = svmm->notifier.mm;
  586. int ret;
  587. ret = mmu_interval_notifier_insert(&notifier->notifier, mm,
  588. args->p.addr, args->p.size,
  589. &nouveau_svm_mni_ops);
  590. if (ret)
  591. return ret;
  592. range.start = notifier->notifier.interval_tree.start;
  593. range.end = notifier->notifier.interval_tree.last + 1;
  594. while (true) {
  595. if (time_after(jiffies, timeout)) {
  596. ret = -EBUSY;
  597. goto out;
  598. }
  599. range.notifier_seq = mmu_interval_read_begin(range.notifier);
  600. mmap_read_lock(mm);
  601. ret = hmm_range_fault(&range);
  602. mmap_read_unlock(mm);
  603. if (ret) {
  604. if (ret == -EBUSY)
  605. continue;
  606. goto out;
  607. }
  608. mutex_lock(&svmm->mutex);
  609. if (mmu_interval_read_retry(range.notifier,
  610. range.notifier_seq)) {
  611. mutex_unlock(&svmm->mutex);
  612. continue;
  613. }
  614. break;
  615. }
  616. nouveau_hmm_convert_pfn(drm, &range, args);
  617. ret = nvif_object_ioctl(&svmm->vmm->vmm.object, args, size, NULL);
  618. mutex_unlock(&svmm->mutex);
  619. out:
  620. mmu_interval_notifier_remove(&notifier->notifier);
  621. return ret;
  622. }
  623. static int
  624. nouveau_svm_fault(struct nvif_notify *notify)
  625. {
  626. struct nouveau_svm_fault_buffer *buffer =
  627. container_of(notify, typeof(*buffer), notify);
  628. struct nouveau_svm *svm =
  629. container_of(buffer, typeof(*svm), buffer[buffer->id]);
  630. struct nvif_object *device = &svm->drm->client.device.object;
  631. struct nouveau_svmm *svmm;
  632. struct {
  633. struct nouveau_pfnmap_args i;
  634. u64 phys[1];
  635. } args;
  636. unsigned long hmm_flags;
  637. u64 inst, start, limit;
  638. int fi, fn;
  639. int replay = 0, atomic = 0, ret;
  640. /* Parse available fault buffer entries into a cache, and update
  641. * the GET pointer so HW can reuse the entries.
  642. */
  643. SVM_DBG(svm, "fault handler");
  644. if (buffer->get == buffer->put) {
  645. buffer->put = nvif_rd32(device, buffer->putaddr);
  646. buffer->get = nvif_rd32(device, buffer->getaddr);
  647. if (buffer->get == buffer->put)
  648. return NVIF_NOTIFY_KEEP;
  649. }
  650. buffer->fault_nr = 0;
  651. SVM_DBG(svm, "get %08x put %08x", buffer->get, buffer->put);
  652. while (buffer->get != buffer->put) {
  653. nouveau_svm_fault_cache(svm, buffer, buffer->get * 0x20);
  654. if (++buffer->get == buffer->entries)
  655. buffer->get = 0;
  656. }
  657. nvif_wr32(device, buffer->getaddr, buffer->get);
  658. SVM_DBG(svm, "%d fault(s) pending", buffer->fault_nr);
  659. /* Sort parsed faults by instance pointer to prevent unnecessary
  660. * instance to SVMM translations, followed by address and access
  661. * type to reduce the amount of work when handling the faults.
  662. */
  663. sort(buffer->fault, buffer->fault_nr, sizeof(*buffer->fault),
  664. nouveau_svm_fault_cmp, NULL);
  665. /* Lookup SVMM structure for each unique instance pointer. */
  666. mutex_lock(&svm->mutex);
  667. for (fi = 0, svmm = NULL; fi < buffer->fault_nr; fi++) {
  668. if (!svmm || buffer->fault[fi]->inst != inst) {
  669. struct nouveau_ivmm *ivmm =
  670. nouveau_ivmm_find(svm, buffer->fault[fi]->inst);
  671. svmm = ivmm ? ivmm->svmm : NULL;
  672. inst = buffer->fault[fi]->inst;
  673. SVM_DBG(svm, "inst %016llx -> svm-%p", inst, svmm);
  674. }
  675. buffer->fault[fi]->svmm = svmm;
  676. }
  677. mutex_unlock(&svm->mutex);
  678. /* Process list of faults. */
  679. args.i.i.version = 0;
  680. args.i.i.type = NVIF_IOCTL_V0_MTHD;
  681. args.i.m.version = 0;
  682. args.i.m.method = NVIF_VMM_V0_PFNMAP;
  683. args.i.p.version = 0;
  684. for (fi = 0; fn = fi + 1, fi < buffer->fault_nr; fi = fn) {
  685. struct svm_notifier notifier;
  686. struct mm_struct *mm;
  687. /* Cancel any faults from non-SVM channels. */
  688. if (!(svmm = buffer->fault[fi]->svmm)) {
  689. nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
  690. continue;
  691. }
  692. SVMM_DBG(svmm, "addr %016llx", buffer->fault[fi]->addr);
  693. /* We try and group handling of faults within a small
  694. * window into a single update.
  695. */
  696. start = buffer->fault[fi]->addr;
  697. limit = start + PAGE_SIZE;
  698. if (start < svmm->unmanaged.limit)
  699. limit = min_t(u64, limit, svmm->unmanaged.start);
  700. /*
  701. * Prepare the GPU-side update of all pages within the
  702. * fault window, determining required pages and access
  703. * permissions based on pending faults.
  704. */
  705. args.i.p.addr = start;
  706. args.i.p.page = PAGE_SHIFT;
  707. args.i.p.size = PAGE_SIZE;
  708. /*
  709. * Determine required permissions based on GPU fault
  710. * access flags.
  711. */
  712. switch (buffer->fault[fi]->access) {
  713. case 0: /* READ. */
  714. hmm_flags = HMM_PFN_REQ_FAULT;
  715. break;
  716. case 2: /* ATOMIC. */
  717. atomic = true;
  718. break;
  719. case 3: /* PREFETCH. */
  720. hmm_flags = 0;
  721. break;
  722. default:
  723. hmm_flags = HMM_PFN_REQ_FAULT | HMM_PFN_REQ_WRITE;
  724. break;
  725. }
  726. mm = svmm->notifier.mm;
  727. if (!mmget_not_zero(mm)) {
  728. nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
  729. continue;
  730. }
  731. notifier.svmm = svmm;
  732. if (atomic)
  733. ret = nouveau_atomic_range_fault(svmm, svm->drm,
  734. &args.i, sizeof(args),
  735. &notifier);
  736. else
  737. ret = nouveau_range_fault(svmm, svm->drm, &args.i,
  738. sizeof(args), hmm_flags,
  739. &notifier);
  740. mmput(mm);
  741. limit = args.i.p.addr + args.i.p.size;
  742. for (fn = fi; ++fn < buffer->fault_nr; ) {
  743. /* It's okay to skip over duplicate addresses from the
  744. * same SVMM as faults are ordered by access type such
  745. * that only the first one needs to be handled.
  746. *
  747. * ie. WRITE faults appear first, thus any handling of
  748. * pending READ faults will already be satisfied.
  749. * But if a large page is mapped, make sure subsequent
  750. * fault addresses have sufficient access permission.
  751. */
  752. if (buffer->fault[fn]->svmm != svmm ||
  753. buffer->fault[fn]->addr >= limit ||
  754. (buffer->fault[fi]->access == FAULT_ACCESS_READ &&
  755. !(args.phys[0] & NVIF_VMM_PFNMAP_V0_V)) ||
  756. (buffer->fault[fi]->access != FAULT_ACCESS_READ &&
  757. buffer->fault[fi]->access != FAULT_ACCESS_PREFETCH &&
  758. !(args.phys[0] & NVIF_VMM_PFNMAP_V0_W)) ||
  759. (buffer->fault[fi]->access != FAULT_ACCESS_READ &&
  760. buffer->fault[fi]->access != FAULT_ACCESS_WRITE &&
  761. buffer->fault[fi]->access != FAULT_ACCESS_PREFETCH &&
  762. !(args.phys[0] & NVIF_VMM_PFNMAP_V0_A)))
  763. break;
  764. }
  765. /* If handling failed completely, cancel all faults. */
  766. if (ret) {
  767. while (fi < fn) {
  768. struct nouveau_svm_fault *fault =
  769. buffer->fault[fi++];
  770. nouveau_svm_fault_cancel_fault(svm, fault);
  771. }
  772. } else
  773. replay++;
  774. }
  775. /* Issue fault replay to the GPU. */
  776. if (replay)
  777. nouveau_svm_fault_replay(svm);
  778. return NVIF_NOTIFY_KEEP;
  779. }
  780. static struct nouveau_pfnmap_args *
  781. nouveau_pfns_to_args(void *pfns)
  782. {
  783. return container_of(pfns, struct nouveau_pfnmap_args, p.phys);
  784. }
  785. u64 *
  786. nouveau_pfns_alloc(unsigned long npages)
  787. {
  788. struct nouveau_pfnmap_args *args;
  789. args = kzalloc(struct_size(args, p.phys, npages), GFP_KERNEL);
  790. if (!args)
  791. return NULL;
  792. args->i.type = NVIF_IOCTL_V0_MTHD;
  793. args->m.method = NVIF_VMM_V0_PFNMAP;
  794. args->p.page = PAGE_SHIFT;
  795. return args->p.phys;
  796. }
  797. void
  798. nouveau_pfns_free(u64 *pfns)
  799. {
  800. struct nouveau_pfnmap_args *args = nouveau_pfns_to_args(pfns);
  801. kfree(args);
  802. }
  803. void
  804. nouveau_pfns_map(struct nouveau_svmm *svmm, struct mm_struct *mm,
  805. unsigned long addr, u64 *pfns, unsigned long npages)
  806. {
  807. struct nouveau_pfnmap_args *args = nouveau_pfns_to_args(pfns);
  808. int ret;
  809. args->p.addr = addr;
  810. args->p.size = npages << PAGE_SHIFT;
  811. mutex_lock(&svmm->mutex);
  812. ret = nvif_object_ioctl(&svmm->vmm->vmm.object, args,
  813. struct_size(args, p.phys, npages), NULL);
  814. mutex_unlock(&svmm->mutex);
  815. }
  816. static void
  817. nouveau_svm_fault_buffer_fini(struct nouveau_svm *svm, int id)
  818. {
  819. struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
  820. nvif_notify_put(&buffer->notify);
  821. }
  822. static int
  823. nouveau_svm_fault_buffer_init(struct nouveau_svm *svm, int id)
  824. {
  825. struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
  826. struct nvif_object *device = &svm->drm->client.device.object;
  827. buffer->get = nvif_rd32(device, buffer->getaddr);
  828. buffer->put = nvif_rd32(device, buffer->putaddr);
  829. SVM_DBG(svm, "get %08x put %08x (init)", buffer->get, buffer->put);
  830. return nvif_notify_get(&buffer->notify);
  831. }
  832. static void
  833. nouveau_svm_fault_buffer_dtor(struct nouveau_svm *svm, int id)
  834. {
  835. struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
  836. int i;
  837. if (buffer->fault) {
  838. for (i = 0; buffer->fault[i] && i < buffer->entries; i++)
  839. kfree(buffer->fault[i]);
  840. kvfree(buffer->fault);
  841. }
  842. nouveau_svm_fault_buffer_fini(svm, id);
  843. nvif_notify_dtor(&buffer->notify);
  844. nvif_object_dtor(&buffer->object);
  845. }
  846. static int
  847. nouveau_svm_fault_buffer_ctor(struct nouveau_svm *svm, s32 oclass, int id)
  848. {
  849. struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
  850. struct nouveau_drm *drm = svm->drm;
  851. struct nvif_object *device = &drm->client.device.object;
  852. struct nvif_clb069_v0 args = {};
  853. int ret;
  854. buffer->id = id;
  855. ret = nvif_object_ctor(device, "svmFaultBuffer", 0, oclass, &args,
  856. sizeof(args), &buffer->object);
  857. if (ret < 0) {
  858. SVM_ERR(svm, "Fault buffer allocation failed: %d", ret);
  859. return ret;
  860. }
  861. nvif_object_map(&buffer->object, NULL, 0);
  862. buffer->entries = args.entries;
  863. buffer->getaddr = args.get;
  864. buffer->putaddr = args.put;
  865. ret = nvif_notify_ctor(&buffer->object, "svmFault", nouveau_svm_fault,
  866. true, NVB069_V0_NTFY_FAULT, NULL, 0, 0,
  867. &buffer->notify);
  868. if (ret)
  869. return ret;
  870. buffer->fault = kvcalloc(sizeof(*buffer->fault), buffer->entries, GFP_KERNEL);
  871. if (!buffer->fault)
  872. return -ENOMEM;
  873. return nouveau_svm_fault_buffer_init(svm, id);
  874. }
  875. void
  876. nouveau_svm_resume(struct nouveau_drm *drm)
  877. {
  878. struct nouveau_svm *svm = drm->svm;
  879. if (svm)
  880. nouveau_svm_fault_buffer_init(svm, 0);
  881. }
  882. void
  883. nouveau_svm_suspend(struct nouveau_drm *drm)
  884. {
  885. struct nouveau_svm *svm = drm->svm;
  886. if (svm)
  887. nouveau_svm_fault_buffer_fini(svm, 0);
  888. }
  889. void
  890. nouveau_svm_fini(struct nouveau_drm *drm)
  891. {
  892. struct nouveau_svm *svm = drm->svm;
  893. if (svm) {
  894. nouveau_svm_fault_buffer_dtor(svm, 0);
  895. kfree(drm->svm);
  896. drm->svm = NULL;
  897. }
  898. }
  899. void
  900. nouveau_svm_init(struct nouveau_drm *drm)
  901. {
  902. static const struct nvif_mclass buffers[] = {
  903. { VOLTA_FAULT_BUFFER_A, 0 },
  904. { MAXWELL_FAULT_BUFFER_A, 0 },
  905. {}
  906. };
  907. struct nouveau_svm *svm;
  908. int ret;
  909. /* Disable on Volta and newer until channel recovery is fixed,
  910. * otherwise clients will have a trivial way to trash the GPU
  911. * for everyone.
  912. */
  913. if (drm->client.device.info.family > NV_DEVICE_INFO_V0_PASCAL)
  914. return;
  915. if (!(drm->svm = svm = kzalloc(sizeof(*drm->svm), GFP_KERNEL)))
  916. return;
  917. drm->svm->drm = drm;
  918. mutex_init(&drm->svm->mutex);
  919. INIT_LIST_HEAD(&drm->svm->inst);
  920. ret = nvif_mclass(&drm->client.device.object, buffers);
  921. if (ret < 0) {
  922. SVM_DBG(svm, "No supported fault buffer class");
  923. nouveau_svm_fini(drm);
  924. return;
  925. }
  926. ret = nouveau_svm_fault_buffer_ctor(svm, buffers[ret].oclass, 0);
  927. if (ret) {
  928. nouveau_svm_fini(drm);
  929. return;
  930. }
  931. SVM_DBG(svm, "Initialised");
  932. }