amd_iommu_v2.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. .flags = MMU_INVALIDATE_DOES_NOT_BLOCK,
  338. .release = mn_release,
  339. .clear_flush_young = mn_clear_flush_young,
  340. .invalidate_range = mn_invalidate_range,
  341. };
  342. static void set_pri_tag_status(struct pasid_state *pasid_state,
  343. u16 tag, int status)
  344. {
  345. unsigned long flags;
  346. spin_lock_irqsave(&pasid_state->lock, flags);
  347. pasid_state->pri[tag].status = status;
  348. spin_unlock_irqrestore(&pasid_state->lock, flags);
  349. }
  350. static void finish_pri_tag(struct device_state *dev_state,
  351. struct pasid_state *pasid_state,
  352. u16 tag)
  353. {
  354. unsigned long flags;
  355. spin_lock_irqsave(&pasid_state->lock, flags);
  356. if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
  357. pasid_state->pri[tag].finish) {
  358. amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
  359. pasid_state->pri[tag].status, tag);
  360. pasid_state->pri[tag].finish = false;
  361. pasid_state->pri[tag].status = PPR_SUCCESS;
  362. }
  363. spin_unlock_irqrestore(&pasid_state->lock, flags);
  364. }
  365. static void handle_fault_error(struct fault *fault)
  366. {
  367. int status;
  368. if (!fault->dev_state->inv_ppr_cb) {
  369. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  370. return;
  371. }
  372. status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
  373. fault->pasid,
  374. fault->address,
  375. fault->flags);
  376. switch (status) {
  377. case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
  378. set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
  379. break;
  380. case AMD_IOMMU_INV_PRI_RSP_INVALID:
  381. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  382. break;
  383. case AMD_IOMMU_INV_PRI_RSP_FAIL:
  384. set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
  385. break;
  386. default:
  387. BUG();
  388. }
  389. }
  390. static bool access_error(struct vm_area_struct *vma, struct fault *fault)
  391. {
  392. unsigned long requested = 0;
  393. if (fault->flags & PPR_FAULT_EXEC)
  394. requested |= VM_EXEC;
  395. if (fault->flags & PPR_FAULT_READ)
  396. requested |= VM_READ;
  397. if (fault->flags & PPR_FAULT_WRITE)
  398. requested |= VM_WRITE;
  399. return (requested & ~vma->vm_flags) != 0;
  400. }
  401. static void do_fault(struct work_struct *work)
  402. {
  403. struct fault *fault = container_of(work, struct fault, work);
  404. struct vm_area_struct *vma;
  405. vm_fault_t ret = VM_FAULT_ERROR;
  406. unsigned int flags = 0;
  407. struct mm_struct *mm;
  408. u64 address;
  409. mm = fault->state->mm;
  410. address = fault->address;
  411. if (fault->flags & PPR_FAULT_USER)
  412. flags |= FAULT_FLAG_USER;
  413. if (fault->flags & PPR_FAULT_WRITE)
  414. flags |= FAULT_FLAG_WRITE;
  415. flags |= FAULT_FLAG_REMOTE;
  416. down_read(&mm->mmap_sem);
  417. vma = find_extend_vma(mm, address);
  418. if (!vma || address < vma->vm_start)
  419. /* failed to get a vma in the right range */
  420. goto out;
  421. /* Check if we have the right permissions on the vma */
  422. if (access_error(vma, fault))
  423. goto out;
  424. ret = handle_mm_fault(vma, address, flags);
  425. out:
  426. up_read(&mm->mmap_sem);
  427. if (ret & VM_FAULT_ERROR)
  428. /* failed to service fault */
  429. handle_fault_error(fault);
  430. finish_pri_tag(fault->dev_state, fault->state, fault->tag);
  431. put_pasid_state(fault->state);
  432. kfree(fault);
  433. }
  434. static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
  435. {
  436. struct amd_iommu_fault *iommu_fault;
  437. struct pasid_state *pasid_state;
  438. struct device_state *dev_state;
  439. unsigned long flags;
  440. struct fault *fault;
  441. bool finish;
  442. u16 tag, devid;
  443. int ret;
  444. struct iommu_dev_data *dev_data;
  445. struct pci_dev *pdev = NULL;
  446. iommu_fault = data;
  447. tag = iommu_fault->tag & 0x1ff;
  448. finish = (iommu_fault->tag >> 9) & 1;
  449. devid = iommu_fault->device_id;
  450. pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
  451. devid & 0xff);
  452. if (!pdev)
  453. return -ENODEV;
  454. dev_data = get_dev_data(&pdev->dev);
  455. /* In kdump kernel pci dev is not initialized yet -> send INVALID */
  456. ret = NOTIFY_DONE;
  457. if (translation_pre_enabled(amd_iommu_rlookup_table[devid])
  458. && dev_data->defer_attach) {
  459. amd_iommu_complete_ppr(pdev, iommu_fault->pasid,
  460. PPR_INVALID, tag);
  461. goto out;
  462. }
  463. dev_state = get_device_state(iommu_fault->device_id);
  464. if (dev_state == NULL)
  465. goto out;
  466. pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
  467. if (pasid_state == NULL || pasid_state->invalid) {
  468. /* We know the device but not the PASID -> send INVALID */
  469. amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
  470. PPR_INVALID, tag);
  471. goto out_drop_state;
  472. }
  473. spin_lock_irqsave(&pasid_state->lock, flags);
  474. atomic_inc(&pasid_state->pri[tag].inflight);
  475. if (finish)
  476. pasid_state->pri[tag].finish = true;
  477. spin_unlock_irqrestore(&pasid_state->lock, flags);
  478. fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
  479. if (fault == NULL) {
  480. /* We are OOM - send success and let the device re-fault */
  481. finish_pri_tag(dev_state, pasid_state, tag);
  482. goto out_drop_state;
  483. }
  484. fault->dev_state = dev_state;
  485. fault->address = iommu_fault->address;
  486. fault->state = pasid_state;
  487. fault->tag = tag;
  488. fault->finish = finish;
  489. fault->pasid = iommu_fault->pasid;
  490. fault->flags = iommu_fault->flags;
  491. INIT_WORK(&fault->work, do_fault);
  492. queue_work(iommu_wq, &fault->work);
  493. ret = NOTIFY_OK;
  494. out_drop_state:
  495. if (ret != NOTIFY_OK && pasid_state)
  496. put_pasid_state(pasid_state);
  497. put_device_state(dev_state);
  498. out:
  499. return ret;
  500. }
  501. static struct notifier_block ppr_nb = {
  502. .notifier_call = ppr_notifier,
  503. };
  504. int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
  505. struct task_struct *task)
  506. {
  507. struct pasid_state *pasid_state;
  508. struct device_state *dev_state;
  509. struct mm_struct *mm;
  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. mm = get_task_mm(task);
  530. pasid_state->mm = mm;
  531. pasid_state->device_state = dev_state;
  532. pasid_state->pasid = pasid;
  533. pasid_state->invalid = true; /* Mark as valid only if we are
  534. done with setting up the pasid */
  535. pasid_state->mn.ops = &iommu_mn;
  536. if (pasid_state->mm == NULL)
  537. goto out_free;
  538. mmu_notifier_register(&pasid_state->mn, mm);
  539. ret = set_pasid_state(dev_state, pasid_state, pasid);
  540. if (ret)
  541. goto out_unregister;
  542. ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
  543. __pa(pasid_state->mm->pgd));
  544. if (ret)
  545. goto out_clear_state;
  546. /* Now we are ready to handle faults */
  547. pasid_state->invalid = false;
  548. /*
  549. * Drop the reference to the mm_struct here. We rely on the
  550. * mmu_notifier release call-back to inform us when the mm
  551. * is going away.
  552. */
  553. mmput(mm);
  554. return 0;
  555. out_clear_state:
  556. clear_pasid_state(dev_state, pasid);
  557. out_unregister:
  558. mmu_notifier_unregister(&pasid_state->mn, mm);
  559. mmput(mm);
  560. out_free:
  561. free_pasid_state(pasid_state);
  562. out:
  563. put_device_state(dev_state);
  564. return ret;
  565. }
  566. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  567. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  568. {
  569. struct pasid_state *pasid_state;
  570. struct device_state *dev_state;
  571. u16 devid;
  572. might_sleep();
  573. if (!amd_iommu_v2_supported())
  574. return;
  575. devid = device_id(pdev);
  576. dev_state = get_device_state(devid);
  577. if (dev_state == NULL)
  578. return;
  579. if (pasid < 0 || pasid >= dev_state->max_pasids)
  580. goto out;
  581. pasid_state = get_pasid_state(dev_state, pasid);
  582. if (pasid_state == NULL)
  583. goto out;
  584. /*
  585. * Drop reference taken here. We are safe because we still hold
  586. * the reference taken in the amd_iommu_bind_pasid function.
  587. */
  588. put_pasid_state(pasid_state);
  589. /* Clear the pasid state so that the pasid can be re-used */
  590. clear_pasid_state(dev_state, pasid_state->pasid);
  591. /*
  592. * Call mmu_notifier_unregister to drop our reference
  593. * to pasid_state->mm
  594. */
  595. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  596. put_pasid_state_wait(pasid_state); /* Reference taken in
  597. amd_iommu_bind_pasid */
  598. out:
  599. /* Drop reference taken in this function */
  600. put_device_state(dev_state);
  601. /* Drop reference taken in amd_iommu_bind_pasid */
  602. put_device_state(dev_state);
  603. }
  604. EXPORT_SYMBOL(amd_iommu_unbind_pasid);
  605. int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
  606. {
  607. struct device_state *dev_state;
  608. struct iommu_group *group;
  609. unsigned long flags;
  610. int ret, tmp;
  611. u16 devid;
  612. might_sleep();
  613. if (!amd_iommu_v2_supported())
  614. return -ENODEV;
  615. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  616. return -EINVAL;
  617. devid = device_id(pdev);
  618. dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
  619. if (dev_state == NULL)
  620. return -ENOMEM;
  621. spin_lock_init(&dev_state->lock);
  622. init_waitqueue_head(&dev_state->wq);
  623. dev_state->pdev = pdev;
  624. dev_state->devid = devid;
  625. tmp = pasids;
  626. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  627. dev_state->pasid_levels += 1;
  628. atomic_set(&dev_state->count, 1);
  629. dev_state->max_pasids = pasids;
  630. ret = -ENOMEM;
  631. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  632. if (dev_state->states == NULL)
  633. goto out_free_dev_state;
  634. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  635. if (dev_state->domain == NULL)
  636. goto out_free_states;
  637. amd_iommu_domain_direct_map(dev_state->domain);
  638. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  639. if (ret)
  640. goto out_free_domain;
  641. group = iommu_group_get(&pdev->dev);
  642. if (!group) {
  643. ret = -EINVAL;
  644. goto out_free_domain;
  645. }
  646. ret = iommu_attach_group(dev_state->domain, group);
  647. if (ret != 0)
  648. goto out_drop_group;
  649. iommu_group_put(group);
  650. spin_lock_irqsave(&state_lock, flags);
  651. if (__get_device_state(devid) != NULL) {
  652. spin_unlock_irqrestore(&state_lock, flags);
  653. ret = -EBUSY;
  654. goto out_free_domain;
  655. }
  656. list_add_tail(&dev_state->list, &state_list);
  657. spin_unlock_irqrestore(&state_lock, flags);
  658. return 0;
  659. out_drop_group:
  660. iommu_group_put(group);
  661. out_free_domain:
  662. iommu_domain_free(dev_state->domain);
  663. out_free_states:
  664. free_page((unsigned long)dev_state->states);
  665. out_free_dev_state:
  666. kfree(dev_state);
  667. return ret;
  668. }
  669. EXPORT_SYMBOL(amd_iommu_init_device);
  670. void amd_iommu_free_device(struct pci_dev *pdev)
  671. {
  672. struct device_state *dev_state;
  673. unsigned long flags;
  674. u16 devid;
  675. if (!amd_iommu_v2_supported())
  676. return;
  677. devid = device_id(pdev);
  678. spin_lock_irqsave(&state_lock, flags);
  679. dev_state = __get_device_state(devid);
  680. if (dev_state == NULL) {
  681. spin_unlock_irqrestore(&state_lock, flags);
  682. return;
  683. }
  684. list_del(&dev_state->list);
  685. spin_unlock_irqrestore(&state_lock, flags);
  686. /* Get rid of any remaining pasid states */
  687. free_pasid_states(dev_state);
  688. put_device_state(dev_state);
  689. /*
  690. * Wait until the last reference is dropped before freeing
  691. * the device state.
  692. */
  693. wait_event(dev_state->wq, !atomic_read(&dev_state->count));
  694. free_device_state(dev_state);
  695. }
  696. EXPORT_SYMBOL(amd_iommu_free_device);
  697. int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  698. amd_iommu_invalid_ppr_cb cb)
  699. {
  700. struct device_state *dev_state;
  701. unsigned long flags;
  702. u16 devid;
  703. int ret;
  704. if (!amd_iommu_v2_supported())
  705. return -ENODEV;
  706. devid = device_id(pdev);
  707. spin_lock_irqsave(&state_lock, flags);
  708. ret = -EINVAL;
  709. dev_state = __get_device_state(devid);
  710. if (dev_state == NULL)
  711. goto out_unlock;
  712. dev_state->inv_ppr_cb = cb;
  713. ret = 0;
  714. out_unlock:
  715. spin_unlock_irqrestore(&state_lock, flags);
  716. return ret;
  717. }
  718. EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
  719. int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  720. amd_iommu_invalidate_ctx cb)
  721. {
  722. struct device_state *dev_state;
  723. unsigned long flags;
  724. u16 devid;
  725. int ret;
  726. if (!amd_iommu_v2_supported())
  727. return -ENODEV;
  728. devid = device_id(pdev);
  729. spin_lock_irqsave(&state_lock, flags);
  730. ret = -EINVAL;
  731. dev_state = __get_device_state(devid);
  732. if (dev_state == NULL)
  733. goto out_unlock;
  734. dev_state->inv_ctx_cb = cb;
  735. ret = 0;
  736. out_unlock:
  737. spin_unlock_irqrestore(&state_lock, flags);
  738. return ret;
  739. }
  740. EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
  741. static int __init amd_iommu_v2_init(void)
  742. {
  743. int ret;
  744. pr_info("AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>\n");
  745. if (!amd_iommu_v2_supported()) {
  746. pr_info("AMD IOMMUv2 functionality not available on this system\n");
  747. /*
  748. * Load anyway to provide the symbols to other modules
  749. * which may use AMD IOMMUv2 optionally.
  750. */
  751. return 0;
  752. }
  753. spin_lock_init(&state_lock);
  754. ret = -ENOMEM;
  755. iommu_wq = alloc_workqueue("amd_iommu_v2", WQ_MEM_RECLAIM, 0);
  756. if (iommu_wq == NULL)
  757. goto out;
  758. amd_iommu_register_ppr_notifier(&ppr_nb);
  759. return 0;
  760. out:
  761. return ret;
  762. }
  763. static void __exit amd_iommu_v2_exit(void)
  764. {
  765. struct device_state *dev_state;
  766. int i;
  767. if (!amd_iommu_v2_supported())
  768. return;
  769. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  770. flush_workqueue(iommu_wq);
  771. /*
  772. * The loop below might call flush_workqueue(), so call
  773. * destroy_workqueue() after it
  774. */
  775. for (i = 0; i < MAX_DEVICES; ++i) {
  776. dev_state = get_device_state(i);
  777. if (dev_state == NULL)
  778. continue;
  779. WARN_ON_ONCE(1);
  780. put_device_state(dev_state);
  781. amd_iommu_free_device(dev_state->pdev);
  782. }
  783. destroy_workqueue(iommu_wq);
  784. }
  785. module_init(amd_iommu_v2_init);
  786. module_exit(amd_iommu_v2_exit);