amd_iommu_v2.c 22 KB

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