amd_iommu_v2.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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, devid;
  442. int ret;
  443. struct iommu_dev_data *dev_data;
  444. struct pci_dev *pdev = NULL;
  445. iommu_fault = data;
  446. tag = iommu_fault->tag & 0x1ff;
  447. finish = (iommu_fault->tag >> 9) & 1;
  448. devid = iommu_fault->device_id;
  449. pdev = pci_get_bus_and_slot(PCI_BUS_NUM(devid), devid & 0xff);
  450. if (!pdev)
  451. return -ENODEV;
  452. dev_data = get_dev_data(&pdev->dev);
  453. /* In kdump kernel pci dev is not initialized yet -> send INVALID */
  454. ret = NOTIFY_DONE;
  455. if (translation_pre_enabled(amd_iommu_rlookup_table[devid])
  456. && dev_data->defer_attach) {
  457. amd_iommu_complete_ppr(pdev, iommu_fault->pasid,
  458. PPR_INVALID, tag);
  459. goto out;
  460. }
  461. dev_state = get_device_state(iommu_fault->device_id);
  462. if (dev_state == NULL)
  463. goto out;
  464. pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
  465. if (pasid_state == NULL || pasid_state->invalid) {
  466. /* We know the device but not the PASID -> send INVALID */
  467. amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
  468. PPR_INVALID, tag);
  469. goto out_drop_state;
  470. }
  471. spin_lock_irqsave(&pasid_state->lock, flags);
  472. atomic_inc(&pasid_state->pri[tag].inflight);
  473. if (finish)
  474. pasid_state->pri[tag].finish = true;
  475. spin_unlock_irqrestore(&pasid_state->lock, flags);
  476. fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
  477. if (fault == NULL) {
  478. /* We are OOM - send success and let the device re-fault */
  479. finish_pri_tag(dev_state, pasid_state, tag);
  480. goto out_drop_state;
  481. }
  482. fault->dev_state = dev_state;
  483. fault->address = iommu_fault->address;
  484. fault->state = pasid_state;
  485. fault->tag = tag;
  486. fault->finish = finish;
  487. fault->pasid = iommu_fault->pasid;
  488. fault->flags = iommu_fault->flags;
  489. INIT_WORK(&fault->work, do_fault);
  490. queue_work(iommu_wq, &fault->work);
  491. ret = NOTIFY_OK;
  492. out_drop_state:
  493. if (ret != NOTIFY_OK && pasid_state)
  494. put_pasid_state(pasid_state);
  495. put_device_state(dev_state);
  496. out:
  497. return ret;
  498. }
  499. static struct notifier_block ppr_nb = {
  500. .notifier_call = ppr_notifier,
  501. };
  502. int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
  503. struct task_struct *task)
  504. {
  505. struct pasid_state *pasid_state;
  506. struct device_state *dev_state;
  507. struct mm_struct *mm;
  508. u16 devid;
  509. int ret;
  510. might_sleep();
  511. if (!amd_iommu_v2_supported())
  512. return -ENODEV;
  513. devid = device_id(pdev);
  514. dev_state = get_device_state(devid);
  515. if (dev_state == NULL)
  516. return -EINVAL;
  517. ret = -EINVAL;
  518. if (pasid < 0 || pasid >= dev_state->max_pasids)
  519. goto out;
  520. ret = -ENOMEM;
  521. pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
  522. if (pasid_state == NULL)
  523. goto out;
  524. atomic_set(&pasid_state->count, 1);
  525. init_waitqueue_head(&pasid_state->wq);
  526. spin_lock_init(&pasid_state->lock);
  527. mm = get_task_mm(task);
  528. pasid_state->mm = mm;
  529. pasid_state->device_state = dev_state;
  530. pasid_state->pasid = pasid;
  531. pasid_state->invalid = true; /* Mark as valid only if we are
  532. done with setting up the 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, 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. /* Now we are ready to handle faults */
  545. pasid_state->invalid = false;
  546. /*
  547. * Drop the reference to the mm_struct here. We rely on the
  548. * mmu_notifier release call-back to inform us when the mm
  549. * is going away.
  550. */
  551. mmput(mm);
  552. return 0;
  553. out_clear_state:
  554. clear_pasid_state(dev_state, pasid);
  555. out_unregister:
  556. mmu_notifier_unregister(&pasid_state->mn, mm);
  557. mmput(mm);
  558. out_free:
  559. free_pasid_state(pasid_state);
  560. out:
  561. put_device_state(dev_state);
  562. return ret;
  563. }
  564. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  565. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  566. {
  567. struct pasid_state *pasid_state;
  568. struct device_state *dev_state;
  569. u16 devid;
  570. might_sleep();
  571. if (!amd_iommu_v2_supported())
  572. return;
  573. devid = device_id(pdev);
  574. dev_state = get_device_state(devid);
  575. if (dev_state == NULL)
  576. return;
  577. if (pasid < 0 || pasid >= dev_state->max_pasids)
  578. goto out;
  579. pasid_state = get_pasid_state(dev_state, pasid);
  580. if (pasid_state == NULL)
  581. goto out;
  582. /*
  583. * Drop reference taken here. We are safe because we still hold
  584. * the reference taken in the amd_iommu_bind_pasid function.
  585. */
  586. put_pasid_state(pasid_state);
  587. /* Clear the pasid state so that the pasid can be re-used */
  588. clear_pasid_state(dev_state, pasid_state->pasid);
  589. /*
  590. * Call mmu_notifier_unregister to drop our reference
  591. * to pasid_state->mm
  592. */
  593. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  594. put_pasid_state_wait(pasid_state); /* Reference taken in
  595. amd_iommu_bind_pasid */
  596. out:
  597. /* Drop reference taken in this function */
  598. put_device_state(dev_state);
  599. /* Drop reference taken in amd_iommu_bind_pasid */
  600. put_device_state(dev_state);
  601. }
  602. EXPORT_SYMBOL(amd_iommu_unbind_pasid);
  603. int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
  604. {
  605. struct device_state *dev_state;
  606. struct iommu_group *group;
  607. unsigned long flags;
  608. int ret, tmp;
  609. u16 devid;
  610. might_sleep();
  611. if (!amd_iommu_v2_supported())
  612. return -ENODEV;
  613. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  614. return -EINVAL;
  615. devid = device_id(pdev);
  616. dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
  617. if (dev_state == NULL)
  618. return -ENOMEM;
  619. spin_lock_init(&dev_state->lock);
  620. init_waitqueue_head(&dev_state->wq);
  621. dev_state->pdev = pdev;
  622. dev_state->devid = devid;
  623. tmp = pasids;
  624. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  625. dev_state->pasid_levels += 1;
  626. atomic_set(&dev_state->count, 1);
  627. dev_state->max_pasids = pasids;
  628. ret = -ENOMEM;
  629. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  630. if (dev_state->states == NULL)
  631. goto out_free_dev_state;
  632. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  633. if (dev_state->domain == NULL)
  634. goto out_free_states;
  635. amd_iommu_domain_direct_map(dev_state->domain);
  636. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  637. if (ret)
  638. goto out_free_domain;
  639. group = iommu_group_get(&pdev->dev);
  640. if (!group) {
  641. ret = -EINVAL;
  642. goto out_free_domain;
  643. }
  644. ret = iommu_attach_group(dev_state->domain, group);
  645. if (ret != 0)
  646. goto out_drop_group;
  647. iommu_group_put(group);
  648. spin_lock_irqsave(&state_lock, flags);
  649. if (__get_device_state(devid) != NULL) {
  650. spin_unlock_irqrestore(&state_lock, flags);
  651. ret = -EBUSY;
  652. goto out_free_domain;
  653. }
  654. list_add_tail(&dev_state->list, &state_list);
  655. spin_unlock_irqrestore(&state_lock, flags);
  656. return 0;
  657. out_drop_group:
  658. iommu_group_put(group);
  659. out_free_domain:
  660. iommu_domain_free(dev_state->domain);
  661. out_free_states:
  662. free_page((unsigned long)dev_state->states);
  663. out_free_dev_state:
  664. kfree(dev_state);
  665. return ret;
  666. }
  667. EXPORT_SYMBOL(amd_iommu_init_device);
  668. void amd_iommu_free_device(struct pci_dev *pdev)
  669. {
  670. struct device_state *dev_state;
  671. unsigned long flags;
  672. u16 devid;
  673. if (!amd_iommu_v2_supported())
  674. return;
  675. devid = device_id(pdev);
  676. spin_lock_irqsave(&state_lock, flags);
  677. dev_state = __get_device_state(devid);
  678. if (dev_state == NULL) {
  679. spin_unlock_irqrestore(&state_lock, flags);
  680. return;
  681. }
  682. list_del(&dev_state->list);
  683. spin_unlock_irqrestore(&state_lock, flags);
  684. /* Get rid of any remaining pasid states */
  685. free_pasid_states(dev_state);
  686. put_device_state(dev_state);
  687. /*
  688. * Wait until the last reference is dropped before freeing
  689. * the device state.
  690. */
  691. wait_event(dev_state->wq, !atomic_read(&dev_state->count));
  692. free_device_state(dev_state);
  693. }
  694. EXPORT_SYMBOL(amd_iommu_free_device);
  695. int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  696. amd_iommu_invalid_ppr_cb cb)
  697. {
  698. struct device_state *dev_state;
  699. unsigned long flags;
  700. u16 devid;
  701. int ret;
  702. if (!amd_iommu_v2_supported())
  703. return -ENODEV;
  704. devid = device_id(pdev);
  705. spin_lock_irqsave(&state_lock, flags);
  706. ret = -EINVAL;
  707. dev_state = __get_device_state(devid);
  708. if (dev_state == NULL)
  709. goto out_unlock;
  710. dev_state->inv_ppr_cb = cb;
  711. ret = 0;
  712. out_unlock:
  713. spin_unlock_irqrestore(&state_lock, flags);
  714. return ret;
  715. }
  716. EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
  717. int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  718. amd_iommu_invalidate_ctx cb)
  719. {
  720. struct device_state *dev_state;
  721. unsigned long flags;
  722. u16 devid;
  723. int ret;
  724. if (!amd_iommu_v2_supported())
  725. return -ENODEV;
  726. devid = device_id(pdev);
  727. spin_lock_irqsave(&state_lock, flags);
  728. ret = -EINVAL;
  729. dev_state = __get_device_state(devid);
  730. if (dev_state == NULL)
  731. goto out_unlock;
  732. dev_state->inv_ctx_cb = cb;
  733. ret = 0;
  734. out_unlock:
  735. spin_unlock_irqrestore(&state_lock, flags);
  736. return ret;
  737. }
  738. EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
  739. static int __init amd_iommu_v2_init(void)
  740. {
  741. int ret;
  742. pr_info("AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>\n");
  743. if (!amd_iommu_v2_supported()) {
  744. pr_info("AMD IOMMUv2 functionality not available on this system\n");
  745. /*
  746. * Load anyway to provide the symbols to other modules
  747. * which may use AMD IOMMUv2 optionally.
  748. */
  749. return 0;
  750. }
  751. spin_lock_init(&state_lock);
  752. ret = -ENOMEM;
  753. iommu_wq = alloc_workqueue("amd_iommu_v2", WQ_MEM_RECLAIM, 0);
  754. if (iommu_wq == NULL)
  755. goto out;
  756. amd_iommu_register_ppr_notifier(&ppr_nb);
  757. return 0;
  758. out:
  759. return ret;
  760. }
  761. static void __exit amd_iommu_v2_exit(void)
  762. {
  763. struct device_state *dev_state;
  764. int i;
  765. if (!amd_iommu_v2_supported())
  766. return;
  767. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  768. flush_workqueue(iommu_wq);
  769. /*
  770. * The loop below might call flush_workqueue(), so call
  771. * destroy_workqueue() after it
  772. */
  773. for (i = 0; i < MAX_DEVICES; ++i) {
  774. dev_state = get_device_state(i);
  775. if (dev_state == NULL)
  776. continue;
  777. WARN_ON_ONCE(1);
  778. put_device_state(dev_state);
  779. amd_iommu_free_device(dev_state->pdev);
  780. }
  781. destroy_workqueue(iommu_wq);
  782. }
  783. module_init(amd_iommu_v2_init);
  784. module_exit(amd_iommu_v2_exit);