amd_iommu_v2.c 22 KB

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