amd_iommu_v2.c 22 KB

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