amd_iommu_v2.c 22 KB

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