amd_iommu_v2.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. int 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_bus_and_slot(PCI_BUS_NUM(devid), devid & 0xff);
  451. if (!pdev)
  452. return -ENODEV;
  453. dev_data = get_dev_data(&pdev->dev);
  454. /* In kdump kernel pci dev is not initialized yet -> send INVALID */
  455. ret = NOTIFY_DONE;
  456. if (translation_pre_enabled(amd_iommu_rlookup_table[devid])
  457. && dev_data->defer_attach) {
  458. amd_iommu_complete_ppr(pdev, iommu_fault->pasid,
  459. PPR_INVALID, tag);
  460. goto out;
  461. }
  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. mmput(mm);
  559. out_free:
  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. struct iommu_group *group;
  608. unsigned long flags;
  609. int ret, tmp;
  610. u16 devid;
  611. might_sleep();
  612. if (!amd_iommu_v2_supported())
  613. return -ENODEV;
  614. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  615. return -EINVAL;
  616. devid = device_id(pdev);
  617. dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
  618. if (dev_state == NULL)
  619. return -ENOMEM;
  620. spin_lock_init(&dev_state->lock);
  621. init_waitqueue_head(&dev_state->wq);
  622. dev_state->pdev = pdev;
  623. dev_state->devid = devid;
  624. tmp = pasids;
  625. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  626. dev_state->pasid_levels += 1;
  627. atomic_set(&dev_state->count, 1);
  628. dev_state->max_pasids = pasids;
  629. ret = -ENOMEM;
  630. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  631. if (dev_state->states == NULL)
  632. goto out_free_dev_state;
  633. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  634. if (dev_state->domain == NULL)
  635. goto out_free_states;
  636. amd_iommu_domain_direct_map(dev_state->domain);
  637. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  638. if (ret)
  639. goto out_free_domain;
  640. group = iommu_group_get(&pdev->dev);
  641. if (!group) {
  642. ret = -EINVAL;
  643. goto out_free_domain;
  644. }
  645. ret = iommu_attach_group(dev_state->domain, group);
  646. if (ret != 0)
  647. goto out_drop_group;
  648. iommu_group_put(group);
  649. spin_lock_irqsave(&state_lock, flags);
  650. if (__get_device_state(devid) != NULL) {
  651. spin_unlock_irqrestore(&state_lock, flags);
  652. ret = -EBUSY;
  653. goto out_free_domain;
  654. }
  655. list_add_tail(&dev_state->list, &state_list);
  656. spin_unlock_irqrestore(&state_lock, flags);
  657. return 0;
  658. out_drop_group:
  659. iommu_group_put(group);
  660. out_free_domain:
  661. iommu_domain_free(dev_state->domain);
  662. out_free_states:
  663. free_page((unsigned long)dev_state->states);
  664. out_free_dev_state:
  665. kfree(dev_state);
  666. return ret;
  667. }
  668. EXPORT_SYMBOL(amd_iommu_init_device);
  669. void amd_iommu_free_device(struct pci_dev *pdev)
  670. {
  671. struct device_state *dev_state;
  672. unsigned long flags;
  673. u16 devid;
  674. if (!amd_iommu_v2_supported())
  675. return;
  676. devid = device_id(pdev);
  677. spin_lock_irqsave(&state_lock, flags);
  678. dev_state = __get_device_state(devid);
  679. if (dev_state == NULL) {
  680. spin_unlock_irqrestore(&state_lock, flags);
  681. return;
  682. }
  683. list_del(&dev_state->list);
  684. spin_unlock_irqrestore(&state_lock, flags);
  685. /* Get rid of any remaining pasid states */
  686. free_pasid_states(dev_state);
  687. put_device_state(dev_state);
  688. /*
  689. * Wait until the last reference is dropped before freeing
  690. * the device state.
  691. */
  692. wait_event(dev_state->wq, !atomic_read(&dev_state->count));
  693. free_device_state(dev_state);
  694. }
  695. EXPORT_SYMBOL(amd_iommu_free_device);
  696. int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
  697. amd_iommu_invalid_ppr_cb cb)
  698. {
  699. struct device_state *dev_state;
  700. unsigned long flags;
  701. u16 devid;
  702. int ret;
  703. if (!amd_iommu_v2_supported())
  704. return -ENODEV;
  705. devid = device_id(pdev);
  706. spin_lock_irqsave(&state_lock, flags);
  707. ret = -EINVAL;
  708. dev_state = __get_device_state(devid);
  709. if (dev_state == NULL)
  710. goto out_unlock;
  711. dev_state->inv_ppr_cb = cb;
  712. ret = 0;
  713. out_unlock:
  714. spin_unlock_irqrestore(&state_lock, flags);
  715. return ret;
  716. }
  717. EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
  718. int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
  719. amd_iommu_invalidate_ctx cb)
  720. {
  721. struct device_state *dev_state;
  722. unsigned long flags;
  723. u16 devid;
  724. int ret;
  725. if (!amd_iommu_v2_supported())
  726. return -ENODEV;
  727. devid = device_id(pdev);
  728. spin_lock_irqsave(&state_lock, flags);
  729. ret = -EINVAL;
  730. dev_state = __get_device_state(devid);
  731. if (dev_state == NULL)
  732. goto out_unlock;
  733. dev_state->inv_ctx_cb = cb;
  734. ret = 0;
  735. out_unlock:
  736. spin_unlock_irqrestore(&state_lock, flags);
  737. return ret;
  738. }
  739. EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
  740. static int __init amd_iommu_v2_init(void)
  741. {
  742. int ret;
  743. pr_info("AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>\n");
  744. if (!amd_iommu_v2_supported()) {
  745. pr_info("AMD IOMMUv2 functionality not available on this system\n");
  746. /*
  747. * Load anyway to provide the symbols to other modules
  748. * which may use AMD IOMMUv2 optionally.
  749. */
  750. return 0;
  751. }
  752. spin_lock_init(&state_lock);
  753. ret = -ENOMEM;
  754. iommu_wq = alloc_workqueue("amd_iommu_v2", WQ_MEM_RECLAIM, 0);
  755. if (iommu_wq == NULL)
  756. goto out;
  757. amd_iommu_register_ppr_notifier(&ppr_nb);
  758. return 0;
  759. out:
  760. return ret;
  761. }
  762. static void __exit amd_iommu_v2_exit(void)
  763. {
  764. struct device_state *dev_state;
  765. int i;
  766. if (!amd_iommu_v2_supported())
  767. return;
  768. amd_iommu_unregister_ppr_notifier(&ppr_nb);
  769. flush_workqueue(iommu_wq);
  770. /*
  771. * The loop below might call flush_workqueue(), so call
  772. * destroy_workqueue() after it
  773. */
  774. for (i = 0; i < MAX_DEVICES; ++i) {
  775. dev_state = get_device_state(i);
  776. if (dev_state == NULL)
  777. continue;
  778. WARN_ON_ONCE(1);
  779. put_device_state(dev_state);
  780. amd_iommu_free_device(dev_state->pdev);
  781. }
  782. destroy_workqueue(iommu_wq);
  783. }
  784. module_init(amd_iommu_v2_init);
  785. module_exit(amd_iommu_v2_exit);