amd_iommu_v2.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * Copyright (C) 2010-2012 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/mmu_notifier.h>
  19. #include <linux/amd-iommu.h>
  20. #include <linux/mm_types.h>
  21. #include <linux/profile.h>
  22. #include <linux/module.h>
  23. #include <linux/sched.h>
  24. #include <linux/iommu.h>
  25. #include <linux/wait.h>
  26. #include <linux/pci.h>
  27. #include <linux/gfp.h>
  28. #include "amd_iommu_types.h"
  29. #include "amd_iommu_proto.h"
  30. MODULE_LICENSE("GPL v2");
  31. MODULE_AUTHOR("Joerg Roedel <joerg.roedel@amd.com>");
  32. #define MAX_DEVICES 0x10000
  33. #define PRI_QUEUE_SIZE 512
  34. struct pri_queue {
  35. atomic_t inflight;
  36. bool finish;
  37. int status;
  38. };
  39. struct pasid_state {
  40. struct list_head list; /* For global state-list */
  41. atomic_t count; /* Reference count */
  42. unsigned mmu_notifier_count; /* Counting nested mmu_notifier
  43. calls */
  44. struct mm_struct *mm; /* mm_struct for the faults */
  45. struct mmu_notifier mn; /* mmu_notifier handle */
  46. struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
  47. struct device_state *device_state; /* Link to our device_state */
  48. int pasid; /* PASID index */
  49. bool invalid; /* Used during setup and
  50. teardown of the pasid */
  51. spinlock_t lock; /* Protect pri_queues and
  52. mmu_notifer_count */
  53. wait_queue_head_t wq; /* To wait for count == 0 */
  54. };
  55. struct device_state {
  56. struct list_head list;
  57. u16 devid;
  58. atomic_t count;
  59. struct pci_dev *pdev;
  60. struct pasid_state **states;
  61. struct iommu_domain *domain;
  62. int pasid_levels;
  63. int max_pasids;
  64. amd_iommu_invalid_ppr_cb inv_ppr_cb;
  65. amd_iommu_invalidate_ctx inv_ctx_cb;
  66. spinlock_t lock;
  67. wait_queue_head_t wq;
  68. };
  69. struct fault {
  70. struct work_struct work;
  71. struct device_state *dev_state;
  72. struct pasid_state *state;
  73. struct mm_struct *mm;
  74. u64 address;
  75. u16 devid;
  76. u16 pasid;
  77. u16 tag;
  78. u16 finish;
  79. u16 flags;
  80. };
  81. static LIST_HEAD(state_list);
  82. static spinlock_t state_lock;
  83. static struct workqueue_struct *iommu_wq;
  84. /*
  85. * Empty page table - Used between
  86. * mmu_notifier_invalidate_range_start and
  87. * mmu_notifier_invalidate_range_end
  88. */
  89. static u64 *empty_page_table;
  90. static void free_pasid_states(struct device_state *dev_state);
  91. static u16 device_id(struct pci_dev *pdev)
  92. {
  93. u16 devid;
  94. devid = pdev->bus->number;
  95. devid = (devid << 8) | pdev->devfn;
  96. return devid;
  97. }
  98. static struct device_state *__get_device_state(u16 devid)
  99. {
  100. struct device_state *dev_state;
  101. list_for_each_entry(dev_state, &state_list, list) {
  102. if (dev_state->devid == devid)
  103. return dev_state;
  104. }
  105. return NULL;
  106. }
  107. static struct device_state *get_device_state(u16 devid)
  108. {
  109. struct device_state *dev_state;
  110. unsigned long flags;
  111. spin_lock_irqsave(&state_lock, flags);
  112. dev_state = __get_device_state(devid);
  113. if (dev_state != NULL)
  114. atomic_inc(&dev_state->count);
  115. spin_unlock_irqrestore(&state_lock, flags);
  116. return dev_state;
  117. }
  118. static void free_device_state(struct device_state *dev_state)
  119. {
  120. /*
  121. * First detach device from domain - No more PRI requests will arrive
  122. * from that device after it is unbound from the IOMMUv2 domain.
  123. */
  124. iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
  125. /* Everything is down now, free the IOMMUv2 domain */
  126. iommu_domain_free(dev_state->domain);
  127. /* Finally get rid of the device-state */
  128. kfree(dev_state);
  129. }
  130. static void put_device_state(struct device_state *dev_state)
  131. {
  132. if (atomic_dec_and_test(&dev_state->count))
  133. wake_up(&dev_state->wq);
  134. }
  135. static void put_device_state_wait(struct device_state *dev_state)
  136. {
  137. DEFINE_WAIT(wait);
  138. prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE);
  139. if (!atomic_dec_and_test(&dev_state->count))
  140. schedule();
  141. finish_wait(&dev_state->wq, &wait);
  142. free_device_state(dev_state);
  143. }
  144. /* Must be called under dev_state->lock */
  145. static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
  146. int pasid, bool alloc)
  147. {
  148. struct pasid_state **root, **ptr;
  149. int level, index;
  150. level = dev_state->pasid_levels;
  151. root = dev_state->states;
  152. while (true) {
  153. index = (pasid >> (9 * level)) & 0x1ff;
  154. ptr = &root[index];
  155. if (level == 0)
  156. break;
  157. if (*ptr == NULL) {
  158. if (!alloc)
  159. return NULL;
  160. *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
  161. if (*ptr == NULL)
  162. return NULL;
  163. }
  164. root = (struct pasid_state **)*ptr;
  165. level -= 1;
  166. }
  167. return ptr;
  168. }
  169. static int set_pasid_state(struct device_state *dev_state,
  170. struct pasid_state *pasid_state,
  171. int pasid)
  172. {
  173. struct pasid_state **ptr;
  174. unsigned long flags;
  175. int ret;
  176. spin_lock_irqsave(&dev_state->lock, flags);
  177. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  178. ret = -ENOMEM;
  179. if (ptr == NULL)
  180. goto out_unlock;
  181. ret = -ENOMEM;
  182. if (*ptr != NULL)
  183. goto out_unlock;
  184. *ptr = pasid_state;
  185. ret = 0;
  186. out_unlock:
  187. spin_unlock_irqrestore(&dev_state->lock, flags);
  188. return ret;
  189. }
  190. static void clear_pasid_state(struct device_state *dev_state, int pasid)
  191. {
  192. struct pasid_state **ptr;
  193. unsigned long flags;
  194. spin_lock_irqsave(&dev_state->lock, flags);
  195. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  196. if (ptr == NULL)
  197. goto out_unlock;
  198. *ptr = NULL;
  199. out_unlock:
  200. spin_unlock_irqrestore(&dev_state->lock, flags);
  201. }
  202. static struct pasid_state *get_pasid_state(struct device_state *dev_state,
  203. int pasid)
  204. {
  205. struct pasid_state **ptr, *ret = NULL;
  206. unsigned long flags;
  207. spin_lock_irqsave(&dev_state->lock, flags);
  208. ptr = __get_pasid_state_ptr(dev_state, pasid, false);
  209. if (ptr == NULL)
  210. goto out_unlock;
  211. ret = *ptr;
  212. if (ret)
  213. atomic_inc(&ret->count);
  214. out_unlock:
  215. spin_unlock_irqrestore(&dev_state->lock, flags);
  216. return ret;
  217. }
  218. static void free_pasid_state(struct pasid_state *pasid_state)
  219. {
  220. kfree(pasid_state);
  221. }
  222. static void put_pasid_state(struct pasid_state *pasid_state)
  223. {
  224. if (atomic_dec_and_test(&pasid_state->count)) {
  225. put_device_state(pasid_state->device_state);
  226. wake_up(&pasid_state->wq);
  227. }
  228. }
  229. static void put_pasid_state_wait(struct pasid_state *pasid_state)
  230. {
  231. DEFINE_WAIT(wait);
  232. prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE);
  233. if (atomic_dec_and_test(&pasid_state->count))
  234. put_device_state(pasid_state->device_state);
  235. else
  236. schedule();
  237. finish_wait(&pasid_state->wq, &wait);
  238. free_pasid_state(pasid_state);
  239. }
  240. static void unbind_pasid(struct pasid_state *pasid_state)
  241. {
  242. struct iommu_domain *domain;
  243. domain = pasid_state->device_state->domain;
  244. /*
  245. * Mark pasid_state as invalid, no more faults will we added to the
  246. * work queue after this is visible everywhere.
  247. */
  248. pasid_state->invalid = true;
  249. /* Make sure this is visible */
  250. smp_wmb();
  251. /* After this the device/pasid can't access the mm anymore */
  252. amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
  253. /* Make sure no more pending faults are in the queue */
  254. flush_workqueue(iommu_wq);
  255. }
  256. static void free_pasid_states_level1(struct pasid_state **tbl)
  257. {
  258. int i;
  259. for (i = 0; i < 512; ++i) {
  260. if (tbl[i] == NULL)
  261. continue;
  262. free_page((unsigned long)tbl[i]);
  263. }
  264. }
  265. static void free_pasid_states_level2(struct pasid_state **tbl)
  266. {
  267. struct pasid_state **ptr;
  268. int i;
  269. for (i = 0; i < 512; ++i) {
  270. if (tbl[i] == NULL)
  271. continue;
  272. ptr = (struct pasid_state **)tbl[i];
  273. free_pasid_states_level1(ptr);
  274. }
  275. }
  276. static void free_pasid_states(struct device_state *dev_state)
  277. {
  278. struct pasid_state *pasid_state;
  279. int i;
  280. for (i = 0; i < dev_state->max_pasids; ++i) {
  281. pasid_state = get_pasid_state(dev_state, i);
  282. if (pasid_state == NULL)
  283. continue;
  284. put_pasid_state(pasid_state);
  285. /*
  286. * This will call the mn_release function and
  287. * unbind the PASID
  288. */
  289. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  290. put_pasid_state_wait(pasid_state); /* Reference taken in
  291. amd_iommu_bind_pasid */
  292. /* Drop reference taken in amd_iommu_bind_pasid */
  293. put_device_state(dev_state);
  294. }
  295. if (dev_state->pasid_levels == 2)
  296. free_pasid_states_level2(dev_state->states);
  297. else if (dev_state->pasid_levels == 1)
  298. free_pasid_states_level1(dev_state->states);
  299. else if (dev_state->pasid_levels != 0)
  300. BUG();
  301. free_page((unsigned long)dev_state->states);
  302. }
  303. static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
  304. {
  305. return container_of(mn, struct pasid_state, mn);
  306. }
  307. static void __mn_flush_page(struct mmu_notifier *mn,
  308. unsigned long address)
  309. {
  310. struct pasid_state *pasid_state;
  311. struct device_state *dev_state;
  312. pasid_state = mn_to_state(mn);
  313. dev_state = pasid_state->device_state;
  314. amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
  315. }
  316. static int mn_clear_flush_young(struct mmu_notifier *mn,
  317. struct mm_struct *mm,
  318. unsigned long start,
  319. unsigned long end)
  320. {
  321. for (; start < end; start += PAGE_SIZE)
  322. __mn_flush_page(mn, start);
  323. return 0;
  324. }
  325. static void mn_invalidate_page(struct mmu_notifier *mn,
  326. struct mm_struct *mm,
  327. unsigned long address)
  328. {
  329. __mn_flush_page(mn, address);
  330. }
  331. static void mn_invalidate_range_start(struct mmu_notifier *mn,
  332. struct mm_struct *mm,
  333. unsigned long start, unsigned long end)
  334. {
  335. struct pasid_state *pasid_state;
  336. struct device_state *dev_state;
  337. unsigned long flags;
  338. pasid_state = mn_to_state(mn);
  339. dev_state = pasid_state->device_state;
  340. spin_lock_irqsave(&pasid_state->lock, flags);
  341. if (pasid_state->mmu_notifier_count == 0) {
  342. amd_iommu_domain_set_gcr3(dev_state->domain,
  343. pasid_state->pasid,
  344. __pa(empty_page_table));
  345. }
  346. pasid_state->mmu_notifier_count += 1;
  347. spin_unlock_irqrestore(&pasid_state->lock, flags);
  348. }
  349. static void mn_invalidate_range_end(struct mmu_notifier *mn,
  350. struct mm_struct *mm,
  351. unsigned long start, unsigned long end)
  352. {
  353. struct pasid_state *pasid_state;
  354. struct device_state *dev_state;
  355. unsigned long flags;
  356. pasid_state = mn_to_state(mn);
  357. dev_state = pasid_state->device_state;
  358. spin_lock_irqsave(&pasid_state->lock, flags);
  359. pasid_state->mmu_notifier_count -= 1;
  360. if (pasid_state->mmu_notifier_count == 0) {
  361. amd_iommu_domain_set_gcr3(dev_state->domain,
  362. pasid_state->pasid,
  363. __pa(pasid_state->mm->pgd));
  364. }
  365. spin_unlock_irqrestore(&pasid_state->lock, flags);
  366. }
  367. static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
  368. {
  369. struct pasid_state *pasid_state;
  370. struct device_state *dev_state;
  371. bool run_inv_ctx_cb;
  372. might_sleep();
  373. pasid_state = mn_to_state(mn);
  374. dev_state = pasid_state->device_state;
  375. run_inv_ctx_cb = !pasid_state->invalid;
  376. if (run_inv_ctx_cb && pasid_state->device_state->inv_ctx_cb)
  377. dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
  378. unbind_pasid(pasid_state);
  379. }
  380. static struct mmu_notifier_ops iommu_mn = {
  381. .release = mn_release,
  382. .clear_flush_young = mn_clear_flush_young,
  383. .invalidate_page = mn_invalidate_page,
  384. .invalidate_range_start = mn_invalidate_range_start,
  385. .invalidate_range_end = mn_invalidate_range_end,
  386. };
  387. static void set_pri_tag_status(struct pasid_state *pasid_state,
  388. u16 tag, int status)
  389. {
  390. unsigned long flags;
  391. spin_lock_irqsave(&pasid_state->lock, flags);
  392. pasid_state->pri[tag].status = status;
  393. spin_unlock_irqrestore(&pasid_state->lock, flags);
  394. }
  395. static void finish_pri_tag(struct device_state *dev_state,
  396. struct pasid_state *pasid_state,
  397. u16 tag)
  398. {
  399. unsigned long flags;
  400. spin_lock_irqsave(&pasid_state->lock, flags);
  401. if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
  402. pasid_state->pri[tag].finish) {
  403. amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
  404. pasid_state->pri[tag].status, tag);
  405. pasid_state->pri[tag].finish = false;
  406. pasid_state->pri[tag].status = PPR_SUCCESS;
  407. }
  408. spin_unlock_irqrestore(&pasid_state->lock, flags);
  409. }
  410. static void do_fault(struct work_struct *work)
  411. {
  412. struct fault *fault = container_of(work, struct fault, work);
  413. int npages, write;
  414. struct page *page;
  415. write = !!(fault->flags & PPR_FAULT_WRITE);
  416. down_read(&fault->state->mm->mmap_sem);
  417. npages = get_user_pages(NULL, fault->state->mm,
  418. fault->address, 1, write, 0, &page, NULL);
  419. up_read(&fault->state->mm->mmap_sem);
  420. if (npages == 1) {
  421. put_page(page);
  422. } else if (fault->dev_state->inv_ppr_cb) {
  423. int status;
  424. status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
  425. fault->pasid,
  426. fault->address,
  427. fault->flags);
  428. switch (status) {
  429. case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
  430. set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
  431. break;
  432. case AMD_IOMMU_INV_PRI_RSP_INVALID:
  433. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  434. break;
  435. case AMD_IOMMU_INV_PRI_RSP_FAIL:
  436. set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
  437. break;
  438. default:
  439. BUG();
  440. }
  441. } else {
  442. set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
  443. }
  444. finish_pri_tag(fault->dev_state, fault->state, fault->tag);
  445. put_pasid_state(fault->state);
  446. kfree(fault);
  447. }
  448. static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
  449. {
  450. struct amd_iommu_fault *iommu_fault;
  451. struct pasid_state *pasid_state;
  452. struct device_state *dev_state;
  453. unsigned long flags;
  454. struct fault *fault;
  455. bool finish;
  456. u16 tag;
  457. int ret;
  458. iommu_fault = data;
  459. tag = iommu_fault->tag & 0x1ff;
  460. finish = (iommu_fault->tag >> 9) & 1;
  461. ret = NOTIFY_DONE;
  462. dev_state = get_device_state(iommu_fault->device_id);
  463. if (dev_state == NULL)
  464. goto out;
  465. pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
  466. if (pasid_state == NULL || pasid_state->invalid) {
  467. /* We know the device but not the PASID -> send INVALID */
  468. amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
  469. PPR_INVALID, tag);
  470. goto out_drop_state;
  471. }
  472. spin_lock_irqsave(&pasid_state->lock, flags);
  473. atomic_inc(&pasid_state->pri[tag].inflight);
  474. if (finish)
  475. pasid_state->pri[tag].finish = true;
  476. spin_unlock_irqrestore(&pasid_state->lock, flags);
  477. fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
  478. if (fault == NULL) {
  479. /* We are OOM - send success and let the device re-fault */
  480. finish_pri_tag(dev_state, pasid_state, tag);
  481. goto out_drop_state;
  482. }
  483. fault->dev_state = dev_state;
  484. fault->address = iommu_fault->address;
  485. fault->state = pasid_state;
  486. fault->tag = tag;
  487. fault->finish = finish;
  488. fault->pasid = iommu_fault->pasid;
  489. fault->flags = iommu_fault->flags;
  490. INIT_WORK(&fault->work, do_fault);
  491. queue_work(iommu_wq, &fault->work);
  492. ret = NOTIFY_OK;
  493. out_drop_state:
  494. if (ret != NOTIFY_OK && pasid_state)
  495. put_pasid_state(pasid_state);
  496. put_device_state(dev_state);
  497. out:
  498. return ret;
  499. }
  500. static struct notifier_block ppr_nb = {
  501. .notifier_call = ppr_notifier,
  502. };
  503. int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
  504. struct task_struct *task)
  505. {
  506. struct pasid_state *pasid_state;
  507. struct device_state *dev_state;
  508. struct mm_struct *mm;
  509. u16 devid;
  510. int ret;
  511. might_sleep();
  512. if (!amd_iommu_v2_supported())
  513. return -ENODEV;
  514. devid = device_id(pdev);
  515. dev_state = get_device_state(devid);
  516. if (dev_state == NULL)
  517. return -EINVAL;
  518. ret = -EINVAL;
  519. if (pasid < 0 || pasid >= dev_state->max_pasids)
  520. goto out;
  521. ret = -ENOMEM;
  522. pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
  523. if (pasid_state == NULL)
  524. goto out;
  525. atomic_set(&pasid_state->count, 1);
  526. init_waitqueue_head(&pasid_state->wq);
  527. spin_lock_init(&pasid_state->lock);
  528. mm = get_task_mm(task);
  529. pasid_state->mm = mm;
  530. pasid_state->device_state = dev_state;
  531. pasid_state->pasid = pasid;
  532. pasid_state->invalid = true; /* Mark as valid only if we are
  533. done with setting up the pasid */
  534. pasid_state->mn.ops = &iommu_mn;
  535. if (pasid_state->mm == NULL)
  536. goto out_free;
  537. mmu_notifier_register(&pasid_state->mn, mm);
  538. ret = set_pasid_state(dev_state, pasid_state, pasid);
  539. if (ret)
  540. goto out_unregister;
  541. ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
  542. __pa(pasid_state->mm->pgd));
  543. if (ret)
  544. goto out_clear_state;
  545. /* Now we are ready to handle faults */
  546. pasid_state->invalid = false;
  547. /*
  548. * Drop the reference to the mm_struct here. We rely on the
  549. * mmu_notifier release call-back to inform us when the mm
  550. * is going away.
  551. */
  552. mmput(mm);
  553. return 0;
  554. out_clear_state:
  555. clear_pasid_state(dev_state, pasid);
  556. out_unregister:
  557. mmu_notifier_unregister(&pasid_state->mn, mm);
  558. out_free:
  559. mmput(mm);
  560. free_pasid_state(pasid_state);
  561. out:
  562. put_device_state(dev_state);
  563. return ret;
  564. }
  565. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  566. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  567. {
  568. struct pasid_state *pasid_state;
  569. struct device_state *dev_state;
  570. u16 devid;
  571. might_sleep();
  572. if (!amd_iommu_v2_supported())
  573. return;
  574. devid = device_id(pdev);
  575. dev_state = get_device_state(devid);
  576. if (dev_state == NULL)
  577. return;
  578. if (pasid < 0 || pasid >= dev_state->max_pasids)
  579. goto out;
  580. pasid_state = get_pasid_state(dev_state, pasid);
  581. if (pasid_state == NULL)
  582. goto out;
  583. /*
  584. * Drop reference taken here. We are safe because we still hold
  585. * the reference taken in the amd_iommu_bind_pasid function.
  586. */
  587. put_pasid_state(pasid_state);
  588. /* Clear the pasid state so that the pasid can be re-used */
  589. clear_pasid_state(dev_state, pasid_state->pasid);
  590. /*
  591. * Call mmu_notifier_unregister to drop our reference
  592. * to pasid_state->mm
  593. */
  594. mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
  595. put_pasid_state_wait(pasid_state); /* Reference taken in
  596. amd_iommu_bind_pasid */
  597. out:
  598. /* Drop reference taken in this function */
  599. put_device_state(dev_state);
  600. /* Drop reference taken in amd_iommu_bind_pasid */
  601. put_device_state(dev_state);
  602. }
  603. EXPORT_SYMBOL(amd_iommu_unbind_pasid);
  604. int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
  605. {
  606. struct device_state *dev_state;
  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. ret = iommu_attach_device(dev_state->domain, &pdev->dev);
  640. if (ret != 0)
  641. goto out_free_domain;
  642. spin_lock_irqsave(&state_lock, flags);
  643. if (__get_device_state(devid) != NULL) {
  644. spin_unlock_irqrestore(&state_lock, flags);
  645. ret = -EBUSY;
  646. goto out_free_domain;
  647. }
  648. list_add_tail(&dev_state->list, &state_list);
  649. spin_unlock_irqrestore(&state_lock, flags);
  650. return 0;
  651. out_free_domain:
  652. iommu_domain_free(dev_state->domain);
  653. out_free_states:
  654. free_page((unsigned long)dev_state->states);
  655. out_free_dev_state:
  656. kfree(dev_state);
  657. return ret;
  658. }
  659. EXPORT_SYMBOL(amd_iommu_init_device);
  660. void amd_iommu_free_device(struct pci_dev *pdev)
  661. {
  662. struct device_state *dev_state;
  663. unsigned long flags;
  664. u16 devid;
  665. if (!amd_iommu_v2_supported())
  666. return;
  667. devid = device_id(pdev);
  668. spin_lock_irqsave(&state_lock, flags);
  669. dev_state = __get_device_state(devid);
  670. if (dev_state == NULL) {
  671. spin_unlock_irqrestore(&state_lock, flags);
  672. return;
  673. }
  674. list_del(&dev_state->list);
  675. spin_unlock_irqrestore(&state_lock, flags);
  676. /* Get rid of any remaining pasid states */
  677. free_pasid_states(dev_state);
  678. put_device_state_wait(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 <joerg.roedel@amd.com>\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 = create_workqueue("amd_iommu_v2");
  740. if (iommu_wq == NULL)
  741. goto out;
  742. ret = -ENOMEM;
  743. empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
  744. if (empty_page_table == NULL)
  745. goto out_destroy_wq;
  746. amd_iommu_register_ppr_notifier(&ppr_nb);
  747. return 0;
  748. out_destroy_wq:
  749. destroy_workqueue(iommu_wq);
  750. out:
  751. return ret;
  752. }
  753. static void __exit amd_iommu_v2_exit(void)
  754. {
  755. struct device_state *dev_state;
  756. int i;
  757. if (!amd_iommu_v2_supported())
  758. return;
  759. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  760. flush_workqueue(iommu_wq);
  761. /*
  762. * The loop below might call flush_workqueue(), so call
  763. * destroy_workqueue() after it
  764. */
  765. for (i = 0; i < MAX_DEVICES; ++i) {
  766. dev_state = get_device_state(i);
  767. if (dev_state == NULL)
  768. continue;
  769. WARN_ON_ONCE(1);
  770. put_device_state(dev_state);
  771. amd_iommu_free_device(dev_state->pdev);
  772. }
  773. destroy_workqueue(iommu_wq);
  774. free_page((unsigned long)empty_page_table);
  775. }
  776. module_init(amd_iommu_v2_init);
  777. module_exit(amd_iommu_v2_exit);