amd_iommu_v2.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. spin_lock_init(&pasid_state->lock);
  544. pasid_state->task = task;
  545. pasid_state->mm = get_task_mm(task);
  546. pasid_state->device_state = dev_state;
  547. pasid_state->pasid = pasid;
  548. pasid_state->mn.ops = &iommu_mn;
  549. if (pasid_state->mm == NULL)
  550. goto out_free;
  551. mmu_notifier_register(&pasid_state->mn, pasid_state->mm);
  552. ret = set_pasid_state(dev_state, pasid_state, pasid);
  553. if (ret)
  554. goto out_unregister;
  555. ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
  556. __pa(pasid_state->mm->pgd));
  557. if (ret)
  558. goto out_clear_state;
  559. link_pasid_state(pasid_state);
  560. return 0;
  561. out_clear_state:
  562. clear_pasid_state(dev_state, pasid);
  563. out_unregister:
  564. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  565. out_free:
  566. free_pasid_state(pasid_state);
  567. out:
  568. put_device_state(dev_state);
  569. return ret;
  570. }
  571. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  572. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  573. {
  574. struct device_state *dev_state;
  575. u16 devid;
  576. might_sleep();
  577. if (!amd_iommu_v2_supported())
  578. return;
  579. devid = device_id(pdev);
  580. dev_state = get_device_state(devid);
  581. if (dev_state == NULL)
  582. return;
  583. if (pasid < 0 || pasid >= dev_state->max_pasids)
  584. goto out;
  585. unbind_pasid(dev_state, pasid);
  586. out:
  587. put_device_state(dev_state);
  588. }
  589. EXPORT_SYMBOL(amd_iommu_unbind_pasid);
  590. int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
  591. {
  592. struct device_state *dev_state;
  593. unsigned long flags;
  594. int ret, tmp;
  595. u16 devid;
  596. might_sleep();
  597. if (!amd_iommu_v2_supported())
  598. return -ENODEV;
  599. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  600. return -EINVAL;
  601. devid = device_id(pdev);
  602. dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
  603. if (dev_state == NULL)
  604. return -ENOMEM;
  605. spin_lock_init(&dev_state->lock);
  606. init_waitqueue_head(&dev_state->wq);
  607. dev_state->pdev = pdev;
  608. tmp = pasids;
  609. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  610. dev_state->pasid_levels += 1;
  611. atomic_set(&dev_state->count, 1);
  612. dev_state->max_pasids = pasids;
  613. ret = -ENOMEM;
  614. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  615. if (dev_state->states == NULL)
  616. goto out_free_dev_state;
  617. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  618. if (dev_state->domain == NULL)
  619. goto out_free_states;
  620. amd_iommu_domain_direct_map(dev_state->domain);
  621. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  622. if (ret)
  623. goto out_free_domain;
  624. ret = iommu_attach_device(dev_state->domain, &pdev->dev);
  625. if (ret != 0)
  626. goto out_free_domain;
  627. spin_lock_irqsave(&state_lock, flags);
  628. if (state_table[devid] != NULL) {
  629. spin_unlock_irqrestore(&state_lock, flags);
  630. ret = -EBUSY;
  631. goto out_free_domain;
  632. }
  633. state_table[devid] = dev_state;
  634. spin_unlock_irqrestore(&state_lock, flags);
  635. return 0;
  636. out_free_domain:
  637. iommu_domain_free(dev_state->domain);
  638. out_free_states:
  639. free_page((unsigned long)dev_state->states);
  640. out_free_dev_state:
  641. kfree(dev_state);
  642. return ret;
  643. }
  644. EXPORT_SYMBOL(amd_iommu_init_device);
  645. void amd_iommu_free_device(struct pci_dev *pdev)
  646. {
  647. struct device_state *dev_state;
  648. unsigned long flags;
  649. u16 devid;
  650. if (!amd_iommu_v2_supported())
  651. return;
  652. devid = device_id(pdev);
  653. spin_lock_irqsave(&state_lock, flags);
  654. dev_state = state_table[devid];
  655. if (dev_state == NULL) {
  656. spin_unlock_irqrestore(&state_lock, flags);
  657. return;
  658. }
  659. state_table[devid] = NULL;
  660. spin_unlock_irqrestore(&state_lock, flags);
  661. /* Get rid of any remaining pasid states */
  662. free_pasid_states(dev_state);
  663. put_device_state_wait(dev_state);
  664. }
  665. EXPORT_SYMBOL(amd_iommu_free_device);
  666. int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  667. amd_iommu_invalid_ppr_cb cb)
  668. {
  669. struct device_state *dev_state;
  670. unsigned long flags;
  671. u16 devid;
  672. int ret;
  673. if (!amd_iommu_v2_supported())
  674. return -ENODEV;
  675. devid = device_id(pdev);
  676. spin_lock_irqsave(&state_lock, flags);
  677. ret = -EINVAL;
  678. dev_state = state_table[devid];
  679. if (dev_state == NULL)
  680. goto out_unlock;
  681. dev_state->inv_ppr_cb = cb;
  682. ret = 0;
  683. out_unlock:
  684. spin_unlock_irqrestore(&state_lock, flags);
  685. return ret;
  686. }
  687. EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
  688. int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  689. amd_iommu_invalidate_ctx cb)
  690. {
  691. struct device_state *dev_state;
  692. unsigned long flags;
  693. u16 devid;
  694. int ret;
  695. if (!amd_iommu_v2_supported())
  696. return -ENODEV;
  697. devid = device_id(pdev);
  698. spin_lock_irqsave(&state_lock, flags);
  699. ret = -EINVAL;
  700. dev_state = state_table[devid];
  701. if (dev_state == NULL)
  702. goto out_unlock;
  703. dev_state->inv_ctx_cb = cb;
  704. ret = 0;
  705. out_unlock:
  706. spin_unlock_irqrestore(&state_lock, flags);
  707. return ret;
  708. }
  709. EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
  710. static int __init amd_iommu_v2_init(void)
  711. {
  712. size_t state_table_size;
  713. int ret;
  714. pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
  715. if (!amd_iommu_v2_supported()) {
  716. pr_info("AMD IOMMUv2 functionality not available on this sytem\n");
  717. /*
  718. * Load anyway to provide the symbols to other modules
  719. * which may use AMD IOMMUv2 optionally.
  720. */
  721. return 0;
  722. }
  723. spin_lock_init(&state_lock);
  724. state_table_size = MAX_DEVICES * sizeof(struct device_state *);
  725. state_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  726. get_order(state_table_size));
  727. if (state_table == NULL)
  728. return -ENOMEM;
  729. ret = -ENOMEM;
  730. iommu_wq = create_workqueue("amd_iommu_v2");
  731. if (iommu_wq == NULL)
  732. goto out_free;
  733. ret = -ENOMEM;
  734. empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
  735. if (empty_page_table == NULL)
  736. goto out_destroy_wq;
  737. amd_iommu_register_ppr_notifier(&ppr_nb);
  738. profile_event_register(PROFILE_TASK_EXIT, &profile_nb);
  739. return 0;
  740. out_destroy_wq:
  741. destroy_workqueue(iommu_wq);
  742. out_free:
  743. free_pages((unsigned long)state_table, get_order(state_table_size));
  744. return ret;
  745. }
  746. static void __exit amd_iommu_v2_exit(void)
  747. {
  748. struct device_state *dev_state;
  749. size_t state_table_size;
  750. int i;
  751. if (!amd_iommu_v2_supported())
  752. return;
  753. profile_event_unregister(PROFILE_TASK_EXIT, &profile_nb);
  754. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  755. flush_workqueue(iommu_wq);
  756. /*
  757. * The loop below might call flush_workqueue(), so call
  758. * destroy_workqueue() after it
  759. */
  760. for (i = 0; i < MAX_DEVICES; ++i) {
  761. dev_state = get_device_state(i);
  762. if (dev_state == NULL)
  763. continue;
  764. WARN_ON_ONCE(1);
  765. put_device_state(dev_state);
  766. amd_iommu_free_device(dev_state->pdev);
  767. }
  768. destroy_workqueue(iommu_wq);
  769. state_table_size = MAX_DEVICES * sizeof(struct device_state *);
  770. free_pages((unsigned long)state_table, get_order(state_table_size));
  771. free_page((unsigned long)empty_page_table);
  772. }
  773. module_init(amd_iommu_v2_init);
  774. module_exit(amd_iommu_v2_exit);