amd_iommu_v2.c 22 KB

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