amd_iommu_v2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 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. /*
  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. free_pasid_state(pasid_state);
  239. }
  240. static void unbind_pasid(struct pasid_state *pasid_state)
  241. {
  242. struct iommu_domain *domain;
  243. domain = pasid_state->device_state->domain;
  244. /*
  245. * Mark pasid_state as invalid, no more faults will we added to the
  246. * work queue after this is visible everywhere.
  247. */
  248. pasid_state->invalid = true;
  249. /* Make sure this is visible */
  250. smp_wmb();
  251. /* After this the device/pasid can't access the mm anymore */
  252. amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
  253. /* Make sure no more pending faults are in the queue */
  254. flush_workqueue(iommu_wq);
  255. }
  256. static void free_pasid_states_level1(struct pasid_state **tbl)
  257. {
  258. int i;
  259. for (i = 0; i < 512; ++i) {
  260. if (tbl[i] == NULL)
  261. continue;
  262. free_page((unsigned long)tbl[i]);
  263. }
  264. }
  265. static void free_pasid_states_level2(struct pasid_state **tbl)
  266. {
  267. struct pasid_state **ptr;
  268. int i;
  269. for (i = 0; i < 512; ++i) {
  270. if (tbl[i] == NULL)
  271. continue;
  272. ptr = (struct pasid_state **)tbl[i];
  273. free_pasid_states_level1(ptr);
  274. }
  275. }
  276. static void free_pasid_states(struct device_state *dev_state)
  277. {
  278. struct pasid_state *pasid_state;
  279. int i;
  280. for (i = 0; i < dev_state->max_pasids; ++i) {
  281. pasid_state = get_pasid_state(dev_state, i);
  282. if (pasid_state == NULL)
  283. continue;
  284. put_pasid_state(pasid_state);
  285. /*
  286. * This will call the mn_release function and
  287. * unbind the PASID
  288. */
  289. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  290. put_pasid_state_wait(pasid_state); /* Reference taken in
  291. amd_iommu_bind_pasid */
  292. /* Drop reference taken in amd_iommu_bind_pasid */
  293. put_device_state(dev_state);
  294. }
  295. if (dev_state->pasid_levels == 2)
  296. free_pasid_states_level2(dev_state->states);
  297. else if (dev_state->pasid_levels == 1)
  298. free_pasid_states_level1(dev_state->states);
  299. else if (dev_state->pasid_levels != 0)
  300. BUG();
  301. free_page((unsigned long)dev_state->states);
  302. }
  303. static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
  304. {
  305. return container_of(mn, struct pasid_state, mn);
  306. }
  307. static void __mn_flush_page(struct mmu_notifier *mn,
  308. unsigned long address)
  309. {
  310. struct pasid_state *pasid_state;
  311. struct device_state *dev_state;
  312. pasid_state = mn_to_state(mn);
  313. dev_state = pasid_state->device_state;
  314. amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
  315. }
  316. static int mn_clear_flush_young(struct mmu_notifier *mn,
  317. struct mm_struct *mm,
  318. unsigned long address)
  319. {
  320. __mn_flush_page(mn, address);
  321. return 0;
  322. }
  323. static void mn_invalidate_page(struct mmu_notifier *mn,
  324. struct mm_struct *mm,
  325. unsigned long address)
  326. {
  327. __mn_flush_page(mn, address);
  328. }
  329. static void mn_invalidate_range_start(struct mmu_notifier *mn,
  330. struct mm_struct *mm,
  331. unsigned long start, unsigned long end)
  332. {
  333. struct pasid_state *pasid_state;
  334. struct device_state *dev_state;
  335. unsigned long flags;
  336. pasid_state = mn_to_state(mn);
  337. dev_state = pasid_state->device_state;
  338. spin_lock_irqsave(&pasid_state->lock, flags);
  339. if (pasid_state->mmu_notifier_count == 0) {
  340. amd_iommu_domain_set_gcr3(dev_state->domain,
  341. pasid_state->pasid,
  342. __pa(empty_page_table));
  343. }
  344. pasid_state->mmu_notifier_count += 1;
  345. spin_unlock_irqrestore(&pasid_state->lock, flags);
  346. }
  347. static void mn_invalidate_range_end(struct mmu_notifier *mn,
  348. struct mm_struct *mm,
  349. unsigned long start, unsigned long end)
  350. {
  351. struct pasid_state *pasid_state;
  352. struct device_state *dev_state;
  353. unsigned long flags;
  354. pasid_state = mn_to_state(mn);
  355. dev_state = pasid_state->device_state;
  356. spin_lock_irqsave(&pasid_state->lock, flags);
  357. pasid_state->mmu_notifier_count -= 1;
  358. if (pasid_state->mmu_notifier_count == 0) {
  359. amd_iommu_domain_set_gcr3(dev_state->domain,
  360. pasid_state->pasid,
  361. __pa(pasid_state->mm->pgd));
  362. }
  363. spin_unlock_irqrestore(&pasid_state->lock, flags);
  364. }
  365. static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
  366. {
  367. struct pasid_state *pasid_state;
  368. struct device_state *dev_state;
  369. bool run_inv_ctx_cb;
  370. might_sleep();
  371. pasid_state = mn_to_state(mn);
  372. dev_state = pasid_state->device_state;
  373. run_inv_ctx_cb = !pasid_state->invalid;
  374. if (run_inv_ctx_cb && pasid_state->device_state->inv_ctx_cb)
  375. dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
  376. unbind_pasid(pasid_state);
  377. }
  378. static struct mmu_notifier_ops iommu_mn = {
  379. .release = mn_release,
  380. .clear_flush_young = mn_clear_flush_young,
  381. .invalidate_page = mn_invalidate_page,
  382. .invalidate_range_start = mn_invalidate_range_start,
  383. .invalidate_range_end = mn_invalidate_range_end,
  384. };
  385. static void set_pri_tag_status(struct pasid_state *pasid_state,
  386. u16 tag, int status)
  387. {
  388. unsigned long flags;
  389. spin_lock_irqsave(&pasid_state->lock, flags);
  390. pasid_state->pri[tag].status = status;
  391. spin_unlock_irqrestore(&pasid_state->lock, flags);
  392. }
  393. static void finish_pri_tag(struct device_state *dev_state,
  394. struct pasid_state *pasid_state,
  395. u16 tag)
  396. {
  397. unsigned long flags;
  398. spin_lock_irqsave(&pasid_state->lock, flags);
  399. if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
  400. pasid_state->pri[tag].finish) {
  401. amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
  402. pasid_state->pri[tag].status, tag);
  403. pasid_state->pri[tag].finish = false;
  404. pasid_state->pri[tag].status = PPR_SUCCESS;
  405. }
  406. spin_unlock_irqrestore(&pasid_state->lock, flags);
  407. }
  408. static void do_fault(struct work_struct *work)
  409. {
  410. struct fault *fault = container_of(work, struct fault, work);
  411. int npages, write;
  412. struct page *page;
  413. write = !!(fault->flags & PPR_FAULT_WRITE);
  414. down_read(&fault->state->mm->mmap_sem);
  415. npages = get_user_pages(NULL, fault->state->mm,
  416. fault->address, 1, write, 0, &page, NULL);
  417. up_read(&fault->state->mm->mmap_sem);
  418. if (npages == 1) {
  419. put_page(page);
  420. } else if (fault->dev_state->inv_ppr_cb) {
  421. int status;
  422. status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
  423. fault->pasid,
  424. fault->address,
  425. fault->flags);
  426. switch (status) {
  427. case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
  428. set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
  429. break;
  430. case AMD_IOMMU_INV_PRI_RSP_INVALID:
  431. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  432. break;
  433. case AMD_IOMMU_INV_PRI_RSP_FAIL:
  434. set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
  435. break;
  436. default:
  437. BUG();
  438. }
  439. } else {
  440. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  441. }
  442. finish_pri_tag(fault->dev_state, fault->state, fault->tag);
  443. put_pasid_state(fault->state);
  444. kfree(fault);
  445. }
  446. static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
  447. {
  448. struct amd_iommu_fault *iommu_fault;
  449. struct pasid_state *pasid_state;
  450. struct device_state *dev_state;
  451. unsigned long flags;
  452. struct fault *fault;
  453. bool finish;
  454. u16 tag;
  455. int ret;
  456. iommu_fault = data;
  457. tag = iommu_fault->tag & 0x1ff;
  458. finish = (iommu_fault->tag >> 9) & 1;
  459. ret = NOTIFY_DONE;
  460. dev_state = get_device_state(iommu_fault->device_id);
  461. if (dev_state == NULL)
  462. goto out;
  463. pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
  464. if (pasid_state == NULL || pasid_state->invalid) {
  465. /* We know the device but not the PASID -> send INVALID */
  466. amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
  467. PPR_INVALID, tag);
  468. goto out_drop_state;
  469. }
  470. spin_lock_irqsave(&pasid_state->lock, flags);
  471. atomic_inc(&pasid_state->pri[tag].inflight);
  472. if (finish)
  473. pasid_state->pri[tag].finish = true;
  474. spin_unlock_irqrestore(&pasid_state->lock, flags);
  475. fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
  476. if (fault == NULL) {
  477. /* We are OOM - send success and let the device re-fault */
  478. finish_pri_tag(dev_state, pasid_state, tag);
  479. goto out_drop_state;
  480. }
  481. fault->dev_state = dev_state;
  482. fault->address = iommu_fault->address;
  483. fault->state = pasid_state;
  484. fault->tag = tag;
  485. fault->finish = finish;
  486. fault->pasid = iommu_fault->pasid;
  487. fault->flags = iommu_fault->flags;
  488. INIT_WORK(&fault->work, do_fault);
  489. queue_work(iommu_wq, &fault->work);
  490. ret = NOTIFY_OK;
  491. out_drop_state:
  492. if (ret != NOTIFY_OK && pasid_state)
  493. put_pasid_state(pasid_state);
  494. put_device_state(dev_state);
  495. out:
  496. return ret;
  497. }
  498. static struct notifier_block ppr_nb = {
  499. .notifier_call = ppr_notifier,
  500. };
  501. int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
  502. struct task_struct *task)
  503. {
  504. struct pasid_state *pasid_state;
  505. struct device_state *dev_state;
  506. struct mm_struct *mm;
  507. u16 devid;
  508. int ret;
  509. might_sleep();
  510. if (!amd_iommu_v2_supported())
  511. return -ENODEV;
  512. devid = device_id(pdev);
  513. dev_state = get_device_state(devid);
  514. if (dev_state == NULL)
  515. return -EINVAL;
  516. ret = -EINVAL;
  517. if (pasid < 0 || pasid >= dev_state->max_pasids)
  518. goto out;
  519. ret = -ENOMEM;
  520. pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
  521. if (pasid_state == NULL)
  522. goto out;
  523. atomic_set(&pasid_state->count, 1);
  524. init_waitqueue_head(&pasid_state->wq);
  525. spin_lock_init(&pasid_state->lock);
  526. mm = get_task_mm(task);
  527. pasid_state->mm = mm;
  528. pasid_state->device_state = dev_state;
  529. pasid_state->pasid = pasid;
  530. pasid_state->invalid = true; /* Mark as valid only if we are
  531. done with setting up the pasid */
  532. pasid_state->mn.ops = &iommu_mn;
  533. if (pasid_state->mm == NULL)
  534. goto out_free;
  535. mmu_notifier_register(&pasid_state->mn, mm);
  536. ret = set_pasid_state(dev_state, pasid_state, pasid);
  537. if (ret)
  538. goto out_unregister;
  539. ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
  540. __pa(pasid_state->mm->pgd));
  541. if (ret)
  542. goto out_clear_state;
  543. /* Now we are ready to handle faults */
  544. pasid_state->invalid = false;
  545. /*
  546. * Drop the reference to the mm_struct here. We rely on the
  547. * mmu_notifier release call-back to inform us when the mm
  548. * is going away.
  549. */
  550. mmput(mm);
  551. return 0;
  552. out_clear_state:
  553. clear_pasid_state(dev_state, pasid);
  554. out_unregister:
  555. mmu_notifier_unregister(&pasid_state->mn, mm);
  556. out_free:
  557. mmput(mm);
  558. free_pasid_state(pasid_state);
  559. out:
  560. put_device_state(dev_state);
  561. return ret;
  562. }
  563. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  564. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  565. {
  566. struct pasid_state *pasid_state;
  567. struct device_state *dev_state;
  568. u16 devid;
  569. might_sleep();
  570. if (!amd_iommu_v2_supported())
  571. return;
  572. devid = device_id(pdev);
  573. dev_state = get_device_state(devid);
  574. if (dev_state == NULL)
  575. return;
  576. if (pasid < 0 || pasid >= dev_state->max_pasids)
  577. goto out;
  578. pasid_state = get_pasid_state(dev_state, pasid);
  579. if (pasid_state == NULL)
  580. goto out;
  581. /*
  582. * Drop reference taken here. We are safe because we still hold
  583. * the reference taken in the amd_iommu_bind_pasid function.
  584. */
  585. put_pasid_state(pasid_state);
  586. /* Clear the pasid state so that the pasid can be re-used */
  587. clear_pasid_state(dev_state, pasid_state->pasid);
  588. /*
  589. * Call mmu_notifier_unregister to drop our reference
  590. * to pasid_state->mm
  591. */
  592. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  593. put_pasid_state_wait(pasid_state); /* Reference taken in
  594. amd_iommu_bind_pasid */
  595. out:
  596. /* Drop reference taken in this function */
  597. put_device_state(dev_state);
  598. /* Drop reference taken in amd_iommu_bind_pasid */
  599. put_device_state(dev_state);
  600. }
  601. EXPORT_SYMBOL(amd_iommu_unbind_pasid);
  602. int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
  603. {
  604. struct device_state *dev_state;
  605. unsigned long flags;
  606. int ret, tmp;
  607. u16 devid;
  608. might_sleep();
  609. if (!amd_iommu_v2_supported())
  610. return -ENODEV;
  611. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  612. return -EINVAL;
  613. devid = device_id(pdev);
  614. dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
  615. if (dev_state == NULL)
  616. return -ENOMEM;
  617. spin_lock_init(&dev_state->lock);
  618. init_waitqueue_head(&dev_state->wq);
  619. dev_state->pdev = pdev;
  620. dev_state->devid = devid;
  621. tmp = pasids;
  622. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  623. dev_state->pasid_levels += 1;
  624. atomic_set(&dev_state->count, 1);
  625. dev_state->max_pasids = pasids;
  626. ret = -ENOMEM;
  627. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  628. if (dev_state->states == NULL)
  629. goto out_free_dev_state;
  630. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  631. if (dev_state->domain == NULL)
  632. goto out_free_states;
  633. amd_iommu_domain_direct_map(dev_state->domain);
  634. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  635. if (ret)
  636. goto out_free_domain;
  637. ret = iommu_attach_device(dev_state->domain, &pdev->dev);
  638. if (ret != 0)
  639. goto out_free_domain;
  640. spin_lock_irqsave(&state_lock, flags);
  641. if (__get_device_state(devid) != NULL) {
  642. spin_unlock_irqrestore(&state_lock, flags);
  643. ret = -EBUSY;
  644. goto out_free_domain;
  645. }
  646. list_add_tail(&dev_state->list, &state_list);
  647. spin_unlock_irqrestore(&state_lock, flags);
  648. return 0;
  649. out_free_domain:
  650. iommu_domain_free(dev_state->domain);
  651. out_free_states:
  652. free_page((unsigned long)dev_state->states);
  653. out_free_dev_state:
  654. kfree(dev_state);
  655. return ret;
  656. }
  657. EXPORT_SYMBOL(amd_iommu_init_device);
  658. void amd_iommu_free_device(struct pci_dev *pdev)
  659. {
  660. struct device_state *dev_state;
  661. unsigned long flags;
  662. u16 devid;
  663. if (!amd_iommu_v2_supported())
  664. return;
  665. devid = device_id(pdev);
  666. spin_lock_irqsave(&state_lock, flags);
  667. dev_state = __get_device_state(devid);
  668. if (dev_state == NULL) {
  669. spin_unlock_irqrestore(&state_lock, flags);
  670. return;
  671. }
  672. list_del(&dev_state->list);
  673. spin_unlock_irqrestore(&state_lock, flags);
  674. /* Get rid of any remaining pasid states */
  675. free_pasid_states(dev_state);
  676. put_device_state_wait(dev_state);
  677. }
  678. EXPORT_SYMBOL(amd_iommu_free_device);
  679. int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  680. amd_iommu_invalid_ppr_cb cb)
  681. {
  682. struct device_state *dev_state;
  683. unsigned long flags;
  684. u16 devid;
  685. int ret;
  686. if (!amd_iommu_v2_supported())
  687. return -ENODEV;
  688. devid = device_id(pdev);
  689. spin_lock_irqsave(&state_lock, flags);
  690. ret = -EINVAL;
  691. dev_state = __get_device_state(devid);
  692. if (dev_state == NULL)
  693. goto out_unlock;
  694. dev_state->inv_ppr_cb = cb;
  695. ret = 0;
  696. out_unlock:
  697. spin_unlock_irqrestore(&state_lock, flags);
  698. return ret;
  699. }
  700. EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
  701. int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  702. amd_iommu_invalidate_ctx cb)
  703. {
  704. struct device_state *dev_state;
  705. unsigned long flags;
  706. u16 devid;
  707. int ret;
  708. if (!amd_iommu_v2_supported())
  709. return -ENODEV;
  710. devid = device_id(pdev);
  711. spin_lock_irqsave(&state_lock, flags);
  712. ret = -EINVAL;
  713. dev_state = __get_device_state(devid);
  714. if (dev_state == NULL)
  715. goto out_unlock;
  716. dev_state->inv_ctx_cb = cb;
  717. ret = 0;
  718. out_unlock:
  719. spin_unlock_irqrestore(&state_lock, flags);
  720. return ret;
  721. }
  722. EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
  723. static int __init amd_iommu_v2_init(void)
  724. {
  725. int ret;
  726. pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
  727. if (!amd_iommu_v2_supported()) {
  728. pr_info("AMD IOMMUv2 functionality not available on this system\n");
  729. /*
  730. * Load anyway to provide the symbols to other modules
  731. * which may use AMD IOMMUv2 optionally.
  732. */
  733. return 0;
  734. }
  735. spin_lock_init(&state_lock);
  736. ret = -ENOMEM;
  737. iommu_wq = create_workqueue("amd_iommu_v2");
  738. if (iommu_wq == NULL)
  739. goto out;
  740. ret = -ENOMEM;
  741. empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
  742. if (empty_page_table == NULL)
  743. goto out_destroy_wq;
  744. amd_iommu_register_ppr_notifier(&ppr_nb);
  745. return 0;
  746. out_destroy_wq:
  747. destroy_workqueue(iommu_wq);
  748. out:
  749. return ret;
  750. }
  751. static void __exit amd_iommu_v2_exit(void)
  752. {
  753. struct device_state *dev_state;
  754. int i;
  755. if (!amd_iommu_v2_supported())
  756. return;
  757. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  758. flush_workqueue(iommu_wq);
  759. /*
  760. * The loop below might call flush_workqueue(), so call
  761. * destroy_workqueue() after it
  762. */
  763. for (i = 0; i < MAX_DEVICES; ++i) {
  764. dev_state = get_device_state(i);
  765. if (dev_state == NULL)
  766. continue;
  767. WARN_ON_ONCE(1);
  768. put_device_state(dev_state);
  769. amd_iommu_free_device(dev_state->pdev);
  770. }
  771. destroy_workqueue(iommu_wq);
  772. free_page((unsigned long)empty_page_table);
  773. }
  774. module_init(amd_iommu_v2_init);
  775. module_exit(amd_iommu_v2_exit);