amd_iommu_v2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * Copyright (C) 2010-2012 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <jroedel@suse.de>
  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/sched/mm.h>
  25. #include <linux/iommu.h>
  26. #include <linux/wait.h>
  27. #include <linux/pci.h>
  28. #include <linux/gfp.h>
  29. #include "amd_iommu_types.h"
  30. #include "amd_iommu_proto.h"
  31. MODULE_LICENSE("GPL v2");
  32. MODULE_AUTHOR("Joerg Roedel <jroedel@suse.de>");
  33. #define MAX_DEVICES 0x10000
  34. #define PRI_QUEUE_SIZE 512
  35. struct pri_queue {
  36. atomic_t inflight;
  37. bool finish;
  38. int status;
  39. };
  40. struct pasid_state {
  41. struct list_head list; /* For global state-list */
  42. atomic_t count; /* Reference count */
  43. unsigned mmu_notifier_count; /* Counting nested mmu_notifier
  44. calls */
  45. struct mm_struct *mm; /* mm_struct for the faults */
  46. struct mmu_notifier mn; /* mmu_notifier 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. bool invalid; /* Used during setup and
  51. teardown of the pasid */
  52. spinlock_t lock; /* Protect pri_queues and
  53. mmu_notifer_count */
  54. wait_queue_head_t wq; /* To wait for count == 0 */
  55. };
  56. struct device_state {
  57. struct list_head list;
  58. u16 devid;
  59. atomic_t count;
  60. struct pci_dev *pdev;
  61. struct pasid_state **states;
  62. struct iommu_domain *domain;
  63. int pasid_levels;
  64. int max_pasids;
  65. amd_iommu_invalid_ppr_cb inv_ppr_cb;
  66. amd_iommu_invalidate_ctx inv_ctx_cb;
  67. spinlock_t lock;
  68. wait_queue_head_t wq;
  69. };
  70. struct fault {
  71. struct work_struct work;
  72. struct device_state *dev_state;
  73. struct pasid_state *state;
  74. struct mm_struct *mm;
  75. u64 address;
  76. u16 devid;
  77. u16 pasid;
  78. u16 tag;
  79. u16 finish;
  80. u16 flags;
  81. };
  82. static LIST_HEAD(state_list);
  83. static spinlock_t state_lock;
  84. static struct workqueue_struct *iommu_wq;
  85. static void free_pasid_states(struct device_state *dev_state);
  86. static u16 device_id(struct pci_dev *pdev)
  87. {
  88. u16 devid;
  89. devid = pdev->bus->number;
  90. devid = (devid << 8) | pdev->devfn;
  91. return devid;
  92. }
  93. static struct device_state *__get_device_state(u16 devid)
  94. {
  95. struct device_state *dev_state;
  96. list_for_each_entry(dev_state, &state_list, list) {
  97. if (dev_state->devid == devid)
  98. return dev_state;
  99. }
  100. return NULL;
  101. }
  102. static struct device_state *get_device_state(u16 devid)
  103. {
  104. struct device_state *dev_state;
  105. unsigned long flags;
  106. spin_lock_irqsave(&state_lock, flags);
  107. dev_state = __get_device_state(devid);
  108. if (dev_state != NULL)
  109. atomic_inc(&dev_state->count);
  110. spin_unlock_irqrestore(&state_lock, flags);
  111. return dev_state;
  112. }
  113. static void free_device_state(struct device_state *dev_state)
  114. {
  115. struct iommu_group *group;
  116. /*
  117. * First detach device from domain - No more PRI requests will arrive
  118. * from that device after it is unbound from the IOMMUv2 domain.
  119. */
  120. group = iommu_group_get(&dev_state->pdev->dev);
  121. if (WARN_ON(!group))
  122. return;
  123. iommu_detach_group(dev_state->domain, group);
  124. iommu_group_put(group);
  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. /* Must be called under dev_state->lock */
  136. static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
  137. int pasid, bool alloc)
  138. {
  139. struct pasid_state **root, **ptr;
  140. int level, index;
  141. level = dev_state->pasid_levels;
  142. root = dev_state->states;
  143. while (true) {
  144. index = (pasid >> (9 * level)) & 0x1ff;
  145. ptr = &root[index];
  146. if (level == 0)
  147. break;
  148. if (*ptr == NULL) {
  149. if (!alloc)
  150. return NULL;
  151. *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
  152. if (*ptr == NULL)
  153. return NULL;
  154. }
  155. root = (struct pasid_state **)*ptr;
  156. level -= 1;
  157. }
  158. return ptr;
  159. }
  160. static int set_pasid_state(struct device_state *dev_state,
  161. struct pasid_state *pasid_state,
  162. int pasid)
  163. {
  164. struct pasid_state **ptr;
  165. unsigned long flags;
  166. int ret;
  167. spin_lock_irqsave(&dev_state->lock, flags);
  168. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  169. ret = -ENOMEM;
  170. if (ptr == NULL)
  171. goto out_unlock;
  172. ret = -ENOMEM;
  173. if (*ptr != NULL)
  174. goto out_unlock;
  175. *ptr = pasid_state;
  176. ret = 0;
  177. out_unlock:
  178. spin_unlock_irqrestore(&dev_state->lock, flags);
  179. return ret;
  180. }
  181. static void clear_pasid_state(struct device_state *dev_state, int pasid)
  182. {
  183. struct pasid_state **ptr;
  184. unsigned long flags;
  185. spin_lock_irqsave(&dev_state->lock, flags);
  186. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  187. if (ptr == NULL)
  188. goto out_unlock;
  189. *ptr = NULL;
  190. out_unlock:
  191. spin_unlock_irqrestore(&dev_state->lock, flags);
  192. }
  193. static struct pasid_state *get_pasid_state(struct device_state *dev_state,
  194. int pasid)
  195. {
  196. struct pasid_state **ptr, *ret = NULL;
  197. unsigned long flags;
  198. spin_lock_irqsave(&dev_state->lock, flags);
  199. ptr = __get_pasid_state_ptr(dev_state, pasid, false);
  200. if (ptr == NULL)
  201. goto out_unlock;
  202. ret = *ptr;
  203. if (ret)
  204. atomic_inc(&ret->count);
  205. out_unlock:
  206. spin_unlock_irqrestore(&dev_state->lock, flags);
  207. return ret;
  208. }
  209. static void free_pasid_state(struct pasid_state *pasid_state)
  210. {
  211. kfree(pasid_state);
  212. }
  213. static void put_pasid_state(struct pasid_state *pasid_state)
  214. {
  215. if (atomic_dec_and_test(&pasid_state->count))
  216. wake_up(&pasid_state->wq);
  217. }
  218. static void put_pasid_state_wait(struct pasid_state *pasid_state)
  219. {
  220. atomic_dec(&pasid_state->count);
  221. wait_event(pasid_state->wq, !atomic_read(&pasid_state->count));
  222. free_pasid_state(pasid_state);
  223. }
  224. static void unbind_pasid(struct pasid_state *pasid_state)
  225. {
  226. struct iommu_domain *domain;
  227. domain = pasid_state->device_state->domain;
  228. /*
  229. * Mark pasid_state as invalid, no more faults will we added to the
  230. * work queue after this is visible everywhere.
  231. */
  232. pasid_state->invalid = true;
  233. /* Make sure this is visible */
  234. smp_wmb();
  235. /* After this the device/pasid can't access the mm anymore */
  236. amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
  237. /* Make sure no more pending faults are in the queue */
  238. flush_workqueue(iommu_wq);
  239. }
  240. static void free_pasid_states_level1(struct pasid_state **tbl)
  241. {
  242. int i;
  243. for (i = 0; i < 512; ++i) {
  244. if (tbl[i] == NULL)
  245. continue;
  246. free_page((unsigned long)tbl[i]);
  247. }
  248. }
  249. static void free_pasid_states_level2(struct pasid_state **tbl)
  250. {
  251. struct pasid_state **ptr;
  252. int i;
  253. for (i = 0; i < 512; ++i) {
  254. if (tbl[i] == NULL)
  255. continue;
  256. ptr = (struct pasid_state **)tbl[i];
  257. free_pasid_states_level1(ptr);
  258. }
  259. }
  260. static void free_pasid_states(struct device_state *dev_state)
  261. {
  262. struct pasid_state *pasid_state;
  263. int i;
  264. for (i = 0; i < dev_state->max_pasids; ++i) {
  265. pasid_state = get_pasid_state(dev_state, i);
  266. if (pasid_state == NULL)
  267. continue;
  268. put_pasid_state(pasid_state);
  269. /*
  270. * This will call the mn_release function and
  271. * unbind the PASID
  272. */
  273. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  274. put_pasid_state_wait(pasid_state); /* Reference taken in
  275. amd_iommu_bind_pasid */
  276. /* Drop reference taken in amd_iommu_bind_pasid */
  277. put_device_state(dev_state);
  278. }
  279. if (dev_state->pasid_levels == 2)
  280. free_pasid_states_level2(dev_state->states);
  281. else if (dev_state->pasid_levels == 1)
  282. free_pasid_states_level1(dev_state->states);
  283. else
  284. BUG_ON(dev_state->pasid_levels != 0);
  285. free_page((unsigned long)dev_state->states);
  286. }
  287. static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
  288. {
  289. return container_of(mn, struct pasid_state, mn);
  290. }
  291. static void __mn_flush_page(struct mmu_notifier *mn,
  292. unsigned long address)
  293. {
  294. struct pasid_state *pasid_state;
  295. struct device_state *dev_state;
  296. pasid_state = mn_to_state(mn);
  297. dev_state = pasid_state->device_state;
  298. amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
  299. }
  300. static int mn_clear_flush_young(struct mmu_notifier *mn,
  301. struct mm_struct *mm,
  302. unsigned long start,
  303. unsigned long end)
  304. {
  305. for (; start < end; start += PAGE_SIZE)
  306. __mn_flush_page(mn, start);
  307. return 0;
  308. }
  309. static void mn_invalidate_range(struct mmu_notifier *mn,
  310. struct mm_struct *mm,
  311. unsigned long start, unsigned long end)
  312. {
  313. struct pasid_state *pasid_state;
  314. struct device_state *dev_state;
  315. pasid_state = mn_to_state(mn);
  316. dev_state = pasid_state->device_state;
  317. if ((start ^ (end - 1)) < PAGE_SIZE)
  318. amd_iommu_flush_page(dev_state->domain, pasid_state->pasid,
  319. start);
  320. else
  321. amd_iommu_flush_tlb(dev_state->domain, pasid_state->pasid);
  322. }
  323. static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
  324. {
  325. struct pasid_state *pasid_state;
  326. struct device_state *dev_state;
  327. bool run_inv_ctx_cb;
  328. might_sleep();
  329. pasid_state = mn_to_state(mn);
  330. dev_state = pasid_state->device_state;
  331. run_inv_ctx_cb = !pasid_state->invalid;
  332. if (run_inv_ctx_cb && dev_state->inv_ctx_cb)
  333. dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
  334. unbind_pasid(pasid_state);
  335. }
  336. static const struct mmu_notifier_ops iommu_mn = {
  337. .release = mn_release,
  338. .clear_flush_young = mn_clear_flush_young,
  339. .invalidate_range = mn_invalidate_range,
  340. };
  341. static void set_pri_tag_status(struct pasid_state *pasid_state,
  342. u16 tag, int status)
  343. {
  344. unsigned long flags;
  345. spin_lock_irqsave(&pasid_state->lock, flags);
  346. pasid_state->pri[tag].status = status;
  347. spin_unlock_irqrestore(&pasid_state->lock, flags);
  348. }
  349. static void finish_pri_tag(struct device_state *dev_state,
  350. struct pasid_state *pasid_state,
  351. u16 tag)
  352. {
  353. unsigned long flags;
  354. spin_lock_irqsave(&pasid_state->lock, flags);
  355. if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
  356. pasid_state->pri[tag].finish) {
  357. amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
  358. pasid_state->pri[tag].status, tag);
  359. pasid_state->pri[tag].finish = false;
  360. pasid_state->pri[tag].status = PPR_SUCCESS;
  361. }
  362. spin_unlock_irqrestore(&pasid_state->lock, flags);
  363. }
  364. static void handle_fault_error(struct fault *fault)
  365. {
  366. int status;
  367. if (!fault->dev_state->inv_ppr_cb) {
  368. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  369. return;
  370. }
  371. status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
  372. fault->pasid,
  373. fault->address,
  374. fault->flags);
  375. switch (status) {
  376. case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
  377. set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
  378. break;
  379. case AMD_IOMMU_INV_PRI_RSP_INVALID:
  380. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  381. break;
  382. case AMD_IOMMU_INV_PRI_RSP_FAIL:
  383. set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
  384. break;
  385. default:
  386. BUG();
  387. }
  388. }
  389. static bool access_error(struct vm_area_struct *vma, struct fault *fault)
  390. {
  391. unsigned long requested = 0;
  392. if (fault->flags & PPR_FAULT_EXEC)
  393. requested |= VM_EXEC;
  394. if (fault->flags & PPR_FAULT_READ)
  395. requested |= VM_READ;
  396. if (fault->flags & PPR_FAULT_WRITE)
  397. requested |= VM_WRITE;
  398. return (requested & ~vma->vm_flags) != 0;
  399. }
  400. static void do_fault(struct work_struct *work)
  401. {
  402. struct fault *fault = container_of(work, struct fault, work);
  403. struct vm_area_struct *vma;
  404. int ret = VM_FAULT_ERROR;
  405. unsigned int flags = 0;
  406. struct mm_struct *mm;
  407. u64 address;
  408. mm = fault->state->mm;
  409. address = fault->address;
  410. if (fault->flags & PPR_FAULT_USER)
  411. flags |= FAULT_FLAG_USER;
  412. if (fault->flags & PPR_FAULT_WRITE)
  413. flags |= FAULT_FLAG_WRITE;
  414. flags |= FAULT_FLAG_REMOTE;
  415. down_read(&mm->mmap_sem);
  416. vma = find_extend_vma(mm, address);
  417. if (!vma || address < vma->vm_start)
  418. /* failed to get a vma in the right range */
  419. goto out;
  420. /* Check if we have the right permissions on the vma */
  421. if (access_error(vma, fault))
  422. goto out;
  423. ret = handle_mm_fault(vma, address, flags);
  424. out:
  425. up_read(&mm->mmap_sem);
  426. if (ret & VM_FAULT_ERROR)
  427. /* failed to service fault */
  428. handle_fault_error(fault);
  429. finish_pri_tag(fault->dev_state, fault->state, fault->tag);
  430. put_pasid_state(fault->state);
  431. kfree(fault);
  432. }
  433. static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
  434. {
  435. struct amd_iommu_fault *iommu_fault;
  436. struct pasid_state *pasid_state;
  437. struct device_state *dev_state;
  438. unsigned long flags;
  439. struct fault *fault;
  440. bool finish;
  441. u16 tag;
  442. int ret;
  443. iommu_fault = data;
  444. tag = iommu_fault->tag & 0x1ff;
  445. finish = (iommu_fault->tag >> 9) & 1;
  446. ret = NOTIFY_DONE;
  447. dev_state = get_device_state(iommu_fault->device_id);
  448. if (dev_state == NULL)
  449. goto out;
  450. pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
  451. if (pasid_state == NULL || pasid_state->invalid) {
  452. /* We know the device but not the PASID -> send INVALID */
  453. amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
  454. PPR_INVALID, tag);
  455. goto out_drop_state;
  456. }
  457. spin_lock_irqsave(&pasid_state->lock, flags);
  458. atomic_inc(&pasid_state->pri[tag].inflight);
  459. if (finish)
  460. pasid_state->pri[tag].finish = true;
  461. spin_unlock_irqrestore(&pasid_state->lock, flags);
  462. fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
  463. if (fault == NULL) {
  464. /* We are OOM - send success and let the device re-fault */
  465. finish_pri_tag(dev_state, pasid_state, tag);
  466. goto out_drop_state;
  467. }
  468. fault->dev_state = dev_state;
  469. fault->address = iommu_fault->address;
  470. fault->state = pasid_state;
  471. fault->tag = tag;
  472. fault->finish = finish;
  473. fault->pasid = iommu_fault->pasid;
  474. fault->flags = iommu_fault->flags;
  475. INIT_WORK(&fault->work, do_fault);
  476. queue_work(iommu_wq, &fault->work);
  477. ret = NOTIFY_OK;
  478. out_drop_state:
  479. if (ret != NOTIFY_OK && pasid_state)
  480. put_pasid_state(pasid_state);
  481. put_device_state(dev_state);
  482. out:
  483. return ret;
  484. }
  485. static struct notifier_block ppr_nb = {
  486. .notifier_call = ppr_notifier,
  487. };
  488. int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
  489. struct task_struct *task)
  490. {
  491. struct pasid_state *pasid_state;
  492. struct device_state *dev_state;
  493. struct mm_struct *mm;
  494. u16 devid;
  495. int ret;
  496. might_sleep();
  497. if (!amd_iommu_v2_supported())
  498. return -ENODEV;
  499. devid = device_id(pdev);
  500. dev_state = get_device_state(devid);
  501. if (dev_state == NULL)
  502. return -EINVAL;
  503. ret = -EINVAL;
  504. if (pasid < 0 || pasid >= dev_state->max_pasids)
  505. goto out;
  506. ret = -ENOMEM;
  507. pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
  508. if (pasid_state == NULL)
  509. goto out;
  510. atomic_set(&pasid_state->count, 1);
  511. init_waitqueue_head(&pasid_state->wq);
  512. spin_lock_init(&pasid_state->lock);
  513. mm = get_task_mm(task);
  514. pasid_state->mm = mm;
  515. pasid_state->device_state = dev_state;
  516. pasid_state->pasid = pasid;
  517. pasid_state->invalid = true; /* Mark as valid only if we are
  518. done with setting up the pasid */
  519. pasid_state->mn.ops = &iommu_mn;
  520. if (pasid_state->mm == NULL)
  521. goto out_free;
  522. mmu_notifier_register(&pasid_state->mn, mm);
  523. ret = set_pasid_state(dev_state, pasid_state, pasid);
  524. if (ret)
  525. goto out_unregister;
  526. ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
  527. __pa(pasid_state->mm->pgd));
  528. if (ret)
  529. goto out_clear_state;
  530. /* Now we are ready to handle faults */
  531. pasid_state->invalid = false;
  532. /*
  533. * Drop the reference to the mm_struct here. We rely on the
  534. * mmu_notifier release call-back to inform us when the mm
  535. * is going away.
  536. */
  537. mmput(mm);
  538. return 0;
  539. out_clear_state:
  540. clear_pasid_state(dev_state, pasid);
  541. out_unregister:
  542. mmu_notifier_unregister(&pasid_state->mn, mm);
  543. mmput(mm);
  544. out_free:
  545. free_pasid_state(pasid_state);
  546. out:
  547. put_device_state(dev_state);
  548. return ret;
  549. }
  550. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  551. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  552. {
  553. struct pasid_state *pasid_state;
  554. struct device_state *dev_state;
  555. u16 devid;
  556. might_sleep();
  557. if (!amd_iommu_v2_supported())
  558. return;
  559. devid = device_id(pdev);
  560. dev_state = get_device_state(devid);
  561. if (dev_state == NULL)
  562. return;
  563. if (pasid < 0 || pasid >= dev_state->max_pasids)
  564. goto out;
  565. pasid_state = get_pasid_state(dev_state, pasid);
  566. if (pasid_state == NULL)
  567. goto out;
  568. /*
  569. * Drop reference taken here. We are safe because we still hold
  570. * the reference taken in the amd_iommu_bind_pasid function.
  571. */
  572. put_pasid_state(pasid_state);
  573. /* Clear the pasid state so that the pasid can be re-used */
  574. clear_pasid_state(dev_state, pasid_state->pasid);
  575. /*
  576. * Call mmu_notifier_unregister to drop our reference
  577. * to pasid_state->mm
  578. */
  579. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  580. put_pasid_state_wait(pasid_state); /* Reference taken in
  581. amd_iommu_bind_pasid */
  582. out:
  583. /* Drop reference taken in this function */
  584. put_device_state(dev_state);
  585. /* Drop reference taken in amd_iommu_bind_pasid */
  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. struct iommu_group *group;
  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. dev_state->devid = devid;
  609. tmp = pasids;
  610. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  611. dev_state->pasid_levels += 1;
  612. atomic_set(&dev_state->count, 1);
  613. dev_state->max_pasids = pasids;
  614. ret = -ENOMEM;
  615. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  616. if (dev_state->states == NULL)
  617. goto out_free_dev_state;
  618. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  619. if (dev_state->domain == NULL)
  620. goto out_free_states;
  621. amd_iommu_domain_direct_map(dev_state->domain);
  622. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  623. if (ret)
  624. goto out_free_domain;
  625. group = iommu_group_get(&pdev->dev);
  626. if (!group) {
  627. ret = -EINVAL;
  628. goto out_free_domain;
  629. }
  630. ret = iommu_attach_group(dev_state->domain, group);
  631. if (ret != 0)
  632. goto out_drop_group;
  633. iommu_group_put(group);
  634. spin_lock_irqsave(&state_lock, flags);
  635. if (__get_device_state(devid) != NULL) {
  636. spin_unlock_irqrestore(&state_lock, flags);
  637. ret = -EBUSY;
  638. goto out_free_domain;
  639. }
  640. list_add_tail(&dev_state->list, &state_list);
  641. spin_unlock_irqrestore(&state_lock, flags);
  642. return 0;
  643. out_drop_group:
  644. iommu_group_put(group);
  645. out_free_domain:
  646. iommu_domain_free(dev_state->domain);
  647. out_free_states:
  648. free_page((unsigned long)dev_state->states);
  649. out_free_dev_state:
  650. kfree(dev_state);
  651. return ret;
  652. }
  653. EXPORT_SYMBOL(amd_iommu_init_device);
  654. void amd_iommu_free_device(struct pci_dev *pdev)
  655. {
  656. struct device_state *dev_state;
  657. unsigned long flags;
  658. u16 devid;
  659. if (!amd_iommu_v2_supported())
  660. return;
  661. devid = device_id(pdev);
  662. spin_lock_irqsave(&state_lock, flags);
  663. dev_state = __get_device_state(devid);
  664. if (dev_state == NULL) {
  665. spin_unlock_irqrestore(&state_lock, flags);
  666. return;
  667. }
  668. list_del(&dev_state->list);
  669. spin_unlock_irqrestore(&state_lock, flags);
  670. /* Get rid of any remaining pasid states */
  671. free_pasid_states(dev_state);
  672. put_device_state(dev_state);
  673. /*
  674. * Wait until the last reference is dropped before freeing
  675. * the device state.
  676. */
  677. wait_event(dev_state->wq, !atomic_read(&dev_state->count));
  678. free_device_state(dev_state);
  679. }
  680. EXPORT_SYMBOL(amd_iommu_free_device);
  681. int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  682. amd_iommu_invalid_ppr_cb cb)
  683. {
  684. struct device_state *dev_state;
  685. unsigned long flags;
  686. u16 devid;
  687. int ret;
  688. if (!amd_iommu_v2_supported())
  689. return -ENODEV;
  690. devid = device_id(pdev);
  691. spin_lock_irqsave(&state_lock, flags);
  692. ret = -EINVAL;
  693. dev_state = __get_device_state(devid);
  694. if (dev_state == NULL)
  695. goto out_unlock;
  696. dev_state->inv_ppr_cb = cb;
  697. ret = 0;
  698. out_unlock:
  699. spin_unlock_irqrestore(&state_lock, flags);
  700. return ret;
  701. }
  702. EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
  703. int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  704. amd_iommu_invalidate_ctx cb)
  705. {
  706. struct device_state *dev_state;
  707. unsigned long flags;
  708. u16 devid;
  709. int ret;
  710. if (!amd_iommu_v2_supported())
  711. return -ENODEV;
  712. devid = device_id(pdev);
  713. spin_lock_irqsave(&state_lock, flags);
  714. ret = -EINVAL;
  715. dev_state = __get_device_state(devid);
  716. if (dev_state == NULL)
  717. goto out_unlock;
  718. dev_state->inv_ctx_cb = cb;
  719. ret = 0;
  720. out_unlock:
  721. spin_unlock_irqrestore(&state_lock, flags);
  722. return ret;
  723. }
  724. EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
  725. static int __init amd_iommu_v2_init(void)
  726. {
  727. int ret;
  728. pr_info("AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>\n");
  729. if (!amd_iommu_v2_supported()) {
  730. pr_info("AMD IOMMUv2 functionality not available on this system\n");
  731. /*
  732. * Load anyway to provide the symbols to other modules
  733. * which may use AMD IOMMUv2 optionally.
  734. */
  735. return 0;
  736. }
  737. spin_lock_init(&state_lock);
  738. ret = -ENOMEM;
  739. iommu_wq = alloc_workqueue("amd_iommu_v2", WQ_MEM_RECLAIM, 0);
  740. if (iommu_wq == NULL)
  741. goto out;
  742. amd_iommu_register_ppr_notifier(&ppr_nb);
  743. return 0;
  744. out:
  745. return ret;
  746. }
  747. static void __exit amd_iommu_v2_exit(void)
  748. {
  749. struct device_state *dev_state;
  750. int i;
  751. if (!amd_iommu_v2_supported())
  752. return;
  753. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  754. flush_workqueue(iommu_wq);
  755. /*
  756. * The loop below might call flush_workqueue(), so call
  757. * destroy_workqueue() after it
  758. */
  759. for (i = 0; i < MAX_DEVICES; ++i) {
  760. dev_state = get_device_state(i);
  761. if (dev_state == NULL)
  762. continue;
  763. WARN_ON_ONCE(1);
  764. put_device_state(dev_state);
  765. amd_iommu_free_device(dev_state->pdev);
  766. }
  767. destroy_workqueue(iommu_wq);
  768. }
  769. module_init(amd_iommu_v2_init);
  770. module_exit(amd_iommu_v2_exit);