amd_iommu_v2.c 22 KB

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