amd_iommu_v2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Copyright (C) 2010-2012 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <jroedel@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/mmu_notifier.h>
  19. #include <linux/amd-iommu.h>
  20. #include <linux/mm_types.h>
  21. #include <linux/profile.h>
  22. #include <linux/module.h>
  23. #include <linux/sched.h>
  24. #include <linux/iommu.h>
  25. #include <linux/wait.h>
  26. #include <linux/pci.h>
  27. #include <linux/gfp.h>
  28. #include "amd_iommu_types.h"
  29. #include "amd_iommu_proto.h"
  30. MODULE_LICENSE("GPL v2");
  31. MODULE_AUTHOR("Joerg Roedel <jroedel@suse.de>");
  32. #define MAX_DEVICES 0x10000
  33. #define PRI_QUEUE_SIZE 512
  34. struct pri_queue {
  35. atomic_t inflight;
  36. bool finish;
  37. int status;
  38. };
  39. struct pasid_state {
  40. struct list_head list; /* For global state-list */
  41. atomic_t count; /* Reference count */
  42. unsigned mmu_notifier_count; /* Counting nested mmu_notifier
  43. calls */
  44. struct mm_struct *mm; /* mm_struct for the faults */
  45. struct mmu_notifier mn; /* mmu_notifier handle */
  46. struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
  47. struct device_state *device_state; /* Link to our device_state */
  48. int pasid; /* PASID index */
  49. bool invalid; /* Used during setup and
  50. teardown of the pasid */
  51. spinlock_t lock; /* Protect pri_queues and
  52. mmu_notifer_count */
  53. wait_queue_head_t wq; /* To wait for count == 0 */
  54. };
  55. struct device_state {
  56. struct list_head list;
  57. u16 devid;
  58. atomic_t count;
  59. struct pci_dev *pdev;
  60. struct pasid_state **states;
  61. struct iommu_domain *domain;
  62. int pasid_levels;
  63. int max_pasids;
  64. amd_iommu_invalid_ppr_cb inv_ppr_cb;
  65. amd_iommu_invalidate_ctx inv_ctx_cb;
  66. spinlock_t lock;
  67. wait_queue_head_t wq;
  68. };
  69. struct fault {
  70. struct work_struct work;
  71. struct device_state *dev_state;
  72. struct pasid_state *state;
  73. struct mm_struct *mm;
  74. u64 address;
  75. u16 devid;
  76. u16 pasid;
  77. u16 tag;
  78. u16 finish;
  79. u16 flags;
  80. };
  81. static LIST_HEAD(state_list);
  82. static spinlock_t state_lock;
  83. static struct workqueue_struct *iommu_wq;
  84. static void free_pasid_states(struct device_state *dev_state);
  85. static u16 device_id(struct pci_dev *pdev)
  86. {
  87. u16 devid;
  88. devid = pdev->bus->number;
  89. devid = (devid << 8) | pdev->devfn;
  90. return devid;
  91. }
  92. static struct device_state *__get_device_state(u16 devid)
  93. {
  94. struct device_state *dev_state;
  95. list_for_each_entry(dev_state, &state_list, list) {
  96. if (dev_state->devid == devid)
  97. return dev_state;
  98. }
  99. return NULL;
  100. }
  101. static struct device_state *get_device_state(u16 devid)
  102. {
  103. struct device_state *dev_state;
  104. unsigned long flags;
  105. spin_lock_irqsave(&state_lock, flags);
  106. dev_state = __get_device_state(devid);
  107. if (dev_state != NULL)
  108. atomic_inc(&dev_state->count);
  109. spin_unlock_irqrestore(&state_lock, flags);
  110. return dev_state;
  111. }
  112. static void free_device_state(struct device_state *dev_state)
  113. {
  114. /*
  115. * First detach device from domain - No more PRI requests will arrive
  116. * from that device after it is unbound from the IOMMUv2 domain.
  117. */
  118. iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
  119. /* Everything is down now, free the IOMMUv2 domain */
  120. iommu_domain_free(dev_state->domain);
  121. /* Finally get rid of the device-state */
  122. kfree(dev_state);
  123. }
  124. static void put_device_state(struct device_state *dev_state)
  125. {
  126. if (atomic_dec_and_test(&dev_state->count))
  127. wake_up(&dev_state->wq);
  128. }
  129. /* Must be called under dev_state->lock */
  130. static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
  131. int pasid, bool alloc)
  132. {
  133. struct pasid_state **root, **ptr;
  134. int level, index;
  135. level = dev_state->pasid_levels;
  136. root = dev_state->states;
  137. while (true) {
  138. index = (pasid >> (9 * level)) & 0x1ff;
  139. ptr = &root[index];
  140. if (level == 0)
  141. break;
  142. if (*ptr == NULL) {
  143. if (!alloc)
  144. return NULL;
  145. *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
  146. if (*ptr == NULL)
  147. return NULL;
  148. }
  149. root = (struct pasid_state **)*ptr;
  150. level -= 1;
  151. }
  152. return ptr;
  153. }
  154. static int set_pasid_state(struct device_state *dev_state,
  155. struct pasid_state *pasid_state,
  156. int pasid)
  157. {
  158. struct pasid_state **ptr;
  159. unsigned long flags;
  160. int ret;
  161. spin_lock_irqsave(&dev_state->lock, flags);
  162. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  163. ret = -ENOMEM;
  164. if (ptr == NULL)
  165. goto out_unlock;
  166. ret = -ENOMEM;
  167. if (*ptr != NULL)
  168. goto out_unlock;
  169. *ptr = pasid_state;
  170. ret = 0;
  171. out_unlock:
  172. spin_unlock_irqrestore(&dev_state->lock, flags);
  173. return ret;
  174. }
  175. static void clear_pasid_state(struct device_state *dev_state, int pasid)
  176. {
  177. struct pasid_state **ptr;
  178. unsigned long flags;
  179. spin_lock_irqsave(&dev_state->lock, flags);
  180. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  181. if (ptr == NULL)
  182. goto out_unlock;
  183. *ptr = NULL;
  184. out_unlock:
  185. spin_unlock_irqrestore(&dev_state->lock, flags);
  186. }
  187. static struct pasid_state *get_pasid_state(struct device_state *dev_state,
  188. int pasid)
  189. {
  190. struct pasid_state **ptr, *ret = NULL;
  191. unsigned long flags;
  192. spin_lock_irqsave(&dev_state->lock, flags);
  193. ptr = __get_pasid_state_ptr(dev_state, pasid, false);
  194. if (ptr == NULL)
  195. goto out_unlock;
  196. ret = *ptr;
  197. if (ret)
  198. atomic_inc(&ret->count);
  199. out_unlock:
  200. spin_unlock_irqrestore(&dev_state->lock, flags);
  201. return ret;
  202. }
  203. static void free_pasid_state(struct pasid_state *pasid_state)
  204. {
  205. kfree(pasid_state);
  206. }
  207. static void put_pasid_state(struct pasid_state *pasid_state)
  208. {
  209. if (atomic_dec_and_test(&pasid_state->count))
  210. wake_up(&pasid_state->wq);
  211. }
  212. static void put_pasid_state_wait(struct pasid_state *pasid_state)
  213. {
  214. atomic_dec(&pasid_state->count);
  215. wait_event(pasid_state->wq, !atomic_read(&pasid_state->count));
  216. free_pasid_state(pasid_state);
  217. }
  218. static void unbind_pasid(struct pasid_state *pasid_state)
  219. {
  220. struct iommu_domain *domain;
  221. domain = pasid_state->device_state->domain;
  222. /*
  223. * Mark pasid_state as invalid, no more faults will we added to the
  224. * work queue after this is visible everywhere.
  225. */
  226. pasid_state->invalid = true;
  227. /* Make sure this is visible */
  228. smp_wmb();
  229. /* After this the device/pasid can't access the mm anymore */
  230. amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
  231. /* Make sure no more pending faults are in the queue */
  232. flush_workqueue(iommu_wq);
  233. }
  234. static void free_pasid_states_level1(struct pasid_state **tbl)
  235. {
  236. int i;
  237. for (i = 0; i < 512; ++i) {
  238. if (tbl[i] == NULL)
  239. continue;
  240. free_page((unsigned long)tbl[i]);
  241. }
  242. }
  243. static void free_pasid_states_level2(struct pasid_state **tbl)
  244. {
  245. struct pasid_state **ptr;
  246. int i;
  247. for (i = 0; i < 512; ++i) {
  248. if (tbl[i] == NULL)
  249. continue;
  250. ptr = (struct pasid_state **)tbl[i];
  251. free_pasid_states_level1(ptr);
  252. }
  253. }
  254. static void free_pasid_states(struct device_state *dev_state)
  255. {
  256. struct pasid_state *pasid_state;
  257. int i;
  258. for (i = 0; i < dev_state->max_pasids; ++i) {
  259. pasid_state = get_pasid_state(dev_state, i);
  260. if (pasid_state == NULL)
  261. continue;
  262. put_pasid_state(pasid_state);
  263. /*
  264. * This will call the mn_release function and
  265. * unbind the PASID
  266. */
  267. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  268. put_pasid_state_wait(pasid_state); /* Reference taken in
  269. amd_iommu_bind_pasid */
  270. /* Drop reference taken in amd_iommu_bind_pasid */
  271. put_device_state(dev_state);
  272. }
  273. if (dev_state->pasid_levels == 2)
  274. free_pasid_states_level2(dev_state->states);
  275. else if (dev_state->pasid_levels == 1)
  276. free_pasid_states_level1(dev_state->states);
  277. else if (dev_state->pasid_levels != 0)
  278. BUG();
  279. free_page((unsigned long)dev_state->states);
  280. }
  281. static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
  282. {
  283. return container_of(mn, struct pasid_state, mn);
  284. }
  285. static void __mn_flush_page(struct mmu_notifier *mn,
  286. unsigned long address)
  287. {
  288. struct pasid_state *pasid_state;
  289. struct device_state *dev_state;
  290. pasid_state = mn_to_state(mn);
  291. dev_state = pasid_state->device_state;
  292. amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
  293. }
  294. static int mn_clear_flush_young(struct mmu_notifier *mn,
  295. struct mm_struct *mm,
  296. unsigned long start,
  297. unsigned long end)
  298. {
  299. for (; start < end; start += PAGE_SIZE)
  300. __mn_flush_page(mn, start);
  301. return 0;
  302. }
  303. static void mn_invalidate_page(struct mmu_notifier *mn,
  304. struct mm_struct *mm,
  305. unsigned long address)
  306. {
  307. __mn_flush_page(mn, address);
  308. }
  309. static void mn_invalidate_range(struct mmu_notifier *mn,
  310. struct mm_struct *mm,
  311. unsigned long start, unsigned long end)
  312. {
  313. struct pasid_state *pasid_state;
  314. struct device_state *dev_state;
  315. pasid_state = mn_to_state(mn);
  316. dev_state = pasid_state->device_state;
  317. if ((start ^ (end - 1)) < PAGE_SIZE)
  318. amd_iommu_flush_page(dev_state->domain, pasid_state->pasid,
  319. start);
  320. else
  321. amd_iommu_flush_tlb(dev_state->domain, pasid_state->pasid);
  322. }
  323. static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
  324. {
  325. struct pasid_state *pasid_state;
  326. struct device_state *dev_state;
  327. bool run_inv_ctx_cb;
  328. might_sleep();
  329. pasid_state = mn_to_state(mn);
  330. dev_state = pasid_state->device_state;
  331. run_inv_ctx_cb = !pasid_state->invalid;
  332. if (run_inv_ctx_cb && dev_state->inv_ctx_cb)
  333. dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
  334. unbind_pasid(pasid_state);
  335. }
  336. static struct mmu_notifier_ops iommu_mn = {
  337. .release = mn_release,
  338. .clear_flush_young = mn_clear_flush_young,
  339. .invalidate_page = mn_invalidate_page,
  340. .invalidate_range = mn_invalidate_range,
  341. };
  342. static void set_pri_tag_status(struct pasid_state *pasid_state,
  343. u16 tag, int status)
  344. {
  345. unsigned long flags;
  346. spin_lock_irqsave(&pasid_state->lock, flags);
  347. pasid_state->pri[tag].status = status;
  348. spin_unlock_irqrestore(&pasid_state->lock, flags);
  349. }
  350. static void finish_pri_tag(struct device_state *dev_state,
  351. struct pasid_state *pasid_state,
  352. u16 tag)
  353. {
  354. unsigned long flags;
  355. spin_lock_irqsave(&pasid_state->lock, flags);
  356. if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
  357. pasid_state->pri[tag].finish) {
  358. amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
  359. pasid_state->pri[tag].status, tag);
  360. pasid_state->pri[tag].finish = false;
  361. pasid_state->pri[tag].status = PPR_SUCCESS;
  362. }
  363. spin_unlock_irqrestore(&pasid_state->lock, flags);
  364. }
  365. static void handle_fault_error(struct fault *fault)
  366. {
  367. int status;
  368. if (!fault->dev_state->inv_ppr_cb) {
  369. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  370. return;
  371. }
  372. status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
  373. fault->pasid,
  374. fault->address,
  375. fault->flags);
  376. switch (status) {
  377. case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
  378. set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
  379. break;
  380. case AMD_IOMMU_INV_PRI_RSP_INVALID:
  381. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  382. break;
  383. case AMD_IOMMU_INV_PRI_RSP_FAIL:
  384. set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
  385. break;
  386. default:
  387. BUG();
  388. }
  389. }
  390. static void do_fault(struct work_struct *work)
  391. {
  392. struct fault *fault = container_of(work, struct fault, work);
  393. struct mm_struct *mm;
  394. struct vm_area_struct *vma;
  395. u64 address;
  396. int ret, write;
  397. write = !!(fault->flags & PPR_FAULT_WRITE);
  398. mm = fault->state->mm;
  399. address = fault->address;
  400. down_read(&mm->mmap_sem);
  401. vma = find_extend_vma(mm, address);
  402. if (!vma || address < vma->vm_start) {
  403. /* failed to get a vma in the right range */
  404. up_read(&mm->mmap_sem);
  405. handle_fault_error(fault);
  406. goto out;
  407. }
  408. ret = handle_mm_fault(mm, vma, address, write);
  409. if (ret & VM_FAULT_ERROR) {
  410. /* failed to service fault */
  411. up_read(&mm->mmap_sem);
  412. handle_fault_error(fault);
  413. goto out;
  414. }
  415. up_read(&mm->mmap_sem);
  416. out:
  417. finish_pri_tag(fault->dev_state, fault->state, fault->tag);
  418. put_pasid_state(fault->state);
  419. kfree(fault);
  420. }
  421. static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
  422. {
  423. struct amd_iommu_fault *iommu_fault;
  424. struct pasid_state *pasid_state;
  425. struct device_state *dev_state;
  426. unsigned long flags;
  427. struct fault *fault;
  428. bool finish;
  429. u16 tag;
  430. int ret;
  431. iommu_fault = data;
  432. tag = iommu_fault->tag & 0x1ff;
  433. finish = (iommu_fault->tag >> 9) & 1;
  434. ret = NOTIFY_DONE;
  435. dev_state = get_device_state(iommu_fault->device_id);
  436. if (dev_state == NULL)
  437. goto out;
  438. pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
  439. if (pasid_state == NULL || pasid_state->invalid) {
  440. /* We know the device but not the PASID -> send INVALID */
  441. amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
  442. PPR_INVALID, tag);
  443. goto out_drop_state;
  444. }
  445. spin_lock_irqsave(&pasid_state->lock, flags);
  446. atomic_inc(&pasid_state->pri[tag].inflight);
  447. if (finish)
  448. pasid_state->pri[tag].finish = true;
  449. spin_unlock_irqrestore(&pasid_state->lock, flags);
  450. fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
  451. if (fault == NULL) {
  452. /* We are OOM - send success and let the device re-fault */
  453. finish_pri_tag(dev_state, pasid_state, tag);
  454. goto out_drop_state;
  455. }
  456. fault->dev_state = dev_state;
  457. fault->address = iommu_fault->address;
  458. fault->state = pasid_state;
  459. fault->tag = tag;
  460. fault->finish = finish;
  461. fault->pasid = iommu_fault->pasid;
  462. fault->flags = iommu_fault->flags;
  463. INIT_WORK(&fault->work, do_fault);
  464. queue_work(iommu_wq, &fault->work);
  465. ret = NOTIFY_OK;
  466. out_drop_state:
  467. if (ret != NOTIFY_OK && pasid_state)
  468. put_pasid_state(pasid_state);
  469. put_device_state(dev_state);
  470. out:
  471. return ret;
  472. }
  473. static struct notifier_block ppr_nb = {
  474. .notifier_call = ppr_notifier,
  475. };
  476. int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
  477. struct task_struct *task)
  478. {
  479. struct pasid_state *pasid_state;
  480. struct device_state *dev_state;
  481. struct mm_struct *mm;
  482. u16 devid;
  483. int ret;
  484. might_sleep();
  485. if (!amd_iommu_v2_supported())
  486. return -ENODEV;
  487. devid = device_id(pdev);
  488. dev_state = get_device_state(devid);
  489. if (dev_state == NULL)
  490. return -EINVAL;
  491. ret = -EINVAL;
  492. if (pasid < 0 || pasid >= dev_state->max_pasids)
  493. goto out;
  494. ret = -ENOMEM;
  495. pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
  496. if (pasid_state == NULL)
  497. goto out;
  498. atomic_set(&pasid_state->count, 1);
  499. init_waitqueue_head(&pasid_state->wq);
  500. spin_lock_init(&pasid_state->lock);
  501. mm = get_task_mm(task);
  502. pasid_state->mm = mm;
  503. pasid_state->device_state = dev_state;
  504. pasid_state->pasid = pasid;
  505. pasid_state->invalid = true; /* Mark as valid only if we are
  506. done with setting up the pasid */
  507. pasid_state->mn.ops = &iommu_mn;
  508. if (pasid_state->mm == NULL)
  509. goto out_free;
  510. mmu_notifier_register(&pasid_state->mn, mm);
  511. ret = set_pasid_state(dev_state, pasid_state, pasid);
  512. if (ret)
  513. goto out_unregister;
  514. ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
  515. __pa(pasid_state->mm->pgd));
  516. if (ret)
  517. goto out_clear_state;
  518. /* Now we are ready to handle faults */
  519. pasid_state->invalid = false;
  520. /*
  521. * Drop the reference to the mm_struct here. We rely on the
  522. * mmu_notifier release call-back to inform us when the mm
  523. * is going away.
  524. */
  525. mmput(mm);
  526. return 0;
  527. out_clear_state:
  528. clear_pasid_state(dev_state, pasid);
  529. out_unregister:
  530. mmu_notifier_unregister(&pasid_state->mn, mm);
  531. out_free:
  532. mmput(mm);
  533. free_pasid_state(pasid_state);
  534. out:
  535. put_device_state(dev_state);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  539. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  540. {
  541. struct pasid_state *pasid_state;
  542. struct device_state *dev_state;
  543. u16 devid;
  544. might_sleep();
  545. if (!amd_iommu_v2_supported())
  546. return;
  547. devid = device_id(pdev);
  548. dev_state = get_device_state(devid);
  549. if (dev_state == NULL)
  550. return;
  551. if (pasid < 0 || pasid >= dev_state->max_pasids)
  552. goto out;
  553. pasid_state = get_pasid_state(dev_state, pasid);
  554. if (pasid_state == NULL)
  555. goto out;
  556. /*
  557. * Drop reference taken here. We are safe because we still hold
  558. * the reference taken in the amd_iommu_bind_pasid function.
  559. */
  560. put_pasid_state(pasid_state);
  561. /* Clear the pasid state so that the pasid can be re-used */
  562. clear_pasid_state(dev_state, pasid_state->pasid);
  563. /*
  564. * Call mmu_notifier_unregister to drop our reference
  565. * to pasid_state->mm
  566. */
  567. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  568. put_pasid_state_wait(pasid_state); /* Reference taken in
  569. amd_iommu_bind_pasid */
  570. out:
  571. /* Drop reference taken in this function */
  572. put_device_state(dev_state);
  573. /* Drop reference taken in amd_iommu_bind_pasid */
  574. put_device_state(dev_state);
  575. }
  576. EXPORT_SYMBOL(amd_iommu_unbind_pasid);
  577. int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
  578. {
  579. struct device_state *dev_state;
  580. unsigned long flags;
  581. int ret, tmp;
  582. u16 devid;
  583. might_sleep();
  584. if (!amd_iommu_v2_supported())
  585. return -ENODEV;
  586. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  587. return -EINVAL;
  588. devid = device_id(pdev);
  589. dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
  590. if (dev_state == NULL)
  591. return -ENOMEM;
  592. spin_lock_init(&dev_state->lock);
  593. init_waitqueue_head(&dev_state->wq);
  594. dev_state->pdev = pdev;
  595. dev_state->devid = devid;
  596. tmp = pasids;
  597. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  598. dev_state->pasid_levels += 1;
  599. atomic_set(&dev_state->count, 1);
  600. dev_state->max_pasids = pasids;
  601. ret = -ENOMEM;
  602. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  603. if (dev_state->states == NULL)
  604. goto out_free_dev_state;
  605. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  606. if (dev_state->domain == NULL)
  607. goto out_free_states;
  608. amd_iommu_domain_direct_map(dev_state->domain);
  609. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  610. if (ret)
  611. goto out_free_domain;
  612. ret = iommu_attach_device(dev_state->domain, &pdev->dev);
  613. if (ret != 0)
  614. goto out_free_domain;
  615. spin_lock_irqsave(&state_lock, flags);
  616. if (__get_device_state(devid) != NULL) {
  617. spin_unlock_irqrestore(&state_lock, flags);
  618. ret = -EBUSY;
  619. goto out_free_domain;
  620. }
  621. list_add_tail(&dev_state->list, &state_list);
  622. spin_unlock_irqrestore(&state_lock, flags);
  623. return 0;
  624. out_free_domain:
  625. iommu_domain_free(dev_state->domain);
  626. out_free_states:
  627. free_page((unsigned long)dev_state->states);
  628. out_free_dev_state:
  629. kfree(dev_state);
  630. return ret;
  631. }
  632. EXPORT_SYMBOL(amd_iommu_init_device);
  633. void amd_iommu_free_device(struct pci_dev *pdev)
  634. {
  635. struct device_state *dev_state;
  636. unsigned long flags;
  637. u16 devid;
  638. if (!amd_iommu_v2_supported())
  639. return;
  640. devid = device_id(pdev);
  641. spin_lock_irqsave(&state_lock, flags);
  642. dev_state = __get_device_state(devid);
  643. if (dev_state == NULL) {
  644. spin_unlock_irqrestore(&state_lock, flags);
  645. return;
  646. }
  647. list_del(&dev_state->list);
  648. spin_unlock_irqrestore(&state_lock, flags);
  649. /* Get rid of any remaining pasid states */
  650. free_pasid_states(dev_state);
  651. put_device_state(dev_state);
  652. /*
  653. * Wait until the last reference is dropped before freeing
  654. * the device state.
  655. */
  656. wait_event(dev_state->wq, !atomic_read(&dev_state->count));
  657. free_device_state(dev_state);
  658. }
  659. EXPORT_SYMBOL(amd_iommu_free_device);
  660. int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  661. amd_iommu_invalid_ppr_cb cb)
  662. {
  663. struct device_state *dev_state;
  664. unsigned long flags;
  665. u16 devid;
  666. int ret;
  667. if (!amd_iommu_v2_supported())
  668. return -ENODEV;
  669. devid = device_id(pdev);
  670. spin_lock_irqsave(&state_lock, flags);
  671. ret = -EINVAL;
  672. dev_state = __get_device_state(devid);
  673. if (dev_state == NULL)
  674. goto out_unlock;
  675. dev_state->inv_ppr_cb = cb;
  676. ret = 0;
  677. out_unlock:
  678. spin_unlock_irqrestore(&state_lock, flags);
  679. return ret;
  680. }
  681. EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
  682. int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  683. amd_iommu_invalidate_ctx cb)
  684. {
  685. struct device_state *dev_state;
  686. unsigned long flags;
  687. u16 devid;
  688. int ret;
  689. if (!amd_iommu_v2_supported())
  690. return -ENODEV;
  691. devid = device_id(pdev);
  692. spin_lock_irqsave(&state_lock, flags);
  693. ret = -EINVAL;
  694. dev_state = __get_device_state(devid);
  695. if (dev_state == NULL)
  696. goto out_unlock;
  697. dev_state->inv_ctx_cb = cb;
  698. ret = 0;
  699. out_unlock:
  700. spin_unlock_irqrestore(&state_lock, flags);
  701. return ret;
  702. }
  703. EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
  704. static int __init amd_iommu_v2_init(void)
  705. {
  706. int ret;
  707. pr_info("AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>\n");
  708. if (!amd_iommu_v2_supported()) {
  709. pr_info("AMD IOMMUv2 functionality not available on this system\n");
  710. /*
  711. * Load anyway to provide the symbols to other modules
  712. * which may use AMD IOMMUv2 optionally.
  713. */
  714. return 0;
  715. }
  716. spin_lock_init(&state_lock);
  717. ret = -ENOMEM;
  718. iommu_wq = create_workqueue("amd_iommu_v2");
  719. if (iommu_wq == NULL)
  720. goto out;
  721. amd_iommu_register_ppr_notifier(&ppr_nb);
  722. return 0;
  723. out:
  724. return ret;
  725. }
  726. static void __exit amd_iommu_v2_exit(void)
  727. {
  728. struct device_state *dev_state;
  729. int i;
  730. if (!amd_iommu_v2_supported())
  731. return;
  732. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  733. flush_workqueue(iommu_wq);
  734. /*
  735. * The loop below might call flush_workqueue(), so call
  736. * destroy_workqueue() after it
  737. */
  738. for (i = 0; i < MAX_DEVICES; ++i) {
  739. dev_state = get_device_state(i);
  740. if (dev_state == NULL)
  741. continue;
  742. WARN_ON_ONCE(1);
  743. put_device_state(dev_state);
  744. amd_iommu_free_device(dev_state->pdev);
  745. }
  746. destroy_workqueue(iommu_wq);
  747. }
  748. module_init(amd_iommu_v2_init);
  749. module_exit(amd_iommu_v2_exit);