amd_iommu_v2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/amd-iommu.h>
  19. #include <linux/mm_types.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/iommu.h>
  23. #include <linux/pci.h>
  24. #include <linux/gfp.h>
  25. #include "amd_iommu_proto.h"
  26. MODULE_LICENSE("GPL v2");
  27. MODULE_AUTHOR("Joerg Roedel <joerg.roedel@amd.com>");
  28. #define MAX_DEVICES 0x10000
  29. #define PRI_QUEUE_SIZE 512
  30. struct pri_queue {
  31. atomic_t inflight;
  32. bool finish;
  33. };
  34. struct pasid_state {
  35. struct list_head list; /* For global state-list */
  36. atomic_t count; /* Reference count */
  37. struct task_struct *task; /* Task bound to this PASID */
  38. struct mm_struct *mm; /* mm_struct for the faults */
  39. struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
  40. struct device_state *device_state; /* Link to our device_state */
  41. int pasid; /* PASID index */
  42. };
  43. struct device_state {
  44. atomic_t count;
  45. struct pci_dev *pdev;
  46. struct pasid_state **states;
  47. struct iommu_domain *domain;
  48. int pasid_levels;
  49. int max_pasids;
  50. spinlock_t lock;
  51. };
  52. struct device_state **state_table;
  53. static spinlock_t state_lock;
  54. /* List and lock for all pasid_states */
  55. static LIST_HEAD(pasid_state_list);
  56. static DEFINE_SPINLOCK(ps_lock);
  57. static void free_pasid_states(struct device_state *dev_state);
  58. static void unbind_pasid(struct device_state *dev_state, int pasid);
  59. static u16 device_id(struct pci_dev *pdev)
  60. {
  61. u16 devid;
  62. devid = pdev->bus->number;
  63. devid = (devid << 8) | pdev->devfn;
  64. return devid;
  65. }
  66. static struct device_state *get_device_state(u16 devid)
  67. {
  68. struct device_state *dev_state;
  69. unsigned long flags;
  70. spin_lock_irqsave(&state_lock, flags);
  71. dev_state = state_table[devid];
  72. if (dev_state != NULL)
  73. atomic_inc(&dev_state->count);
  74. spin_unlock_irqrestore(&state_lock, flags);
  75. return dev_state;
  76. }
  77. static void free_device_state(struct device_state *dev_state)
  78. {
  79. /*
  80. * First detach device from domain - No more PRI requests will arrive
  81. * from that device after it is unbound from the IOMMUv2 domain.
  82. */
  83. iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
  84. /* Everything is down now, free the IOMMUv2 domain */
  85. iommu_domain_free(dev_state->domain);
  86. /* Finally get rid of the device-state */
  87. kfree(dev_state);
  88. }
  89. static void put_device_state(struct device_state *dev_state)
  90. {
  91. if (atomic_dec_and_test(&dev_state->count))
  92. free_device_state(dev_state);
  93. }
  94. static void link_pasid_state(struct pasid_state *pasid_state)
  95. {
  96. spin_lock(&ps_lock);
  97. list_add_tail(&pasid_state->list, &pasid_state_list);
  98. spin_unlock(&ps_lock);
  99. }
  100. static void __unlink_pasid_state(struct pasid_state *pasid_state)
  101. {
  102. list_del(&pasid_state->list);
  103. }
  104. static void unlink_pasid_state(struct pasid_state *pasid_state)
  105. {
  106. spin_lock(&ps_lock);
  107. __unlink_pasid_state(pasid_state);
  108. spin_unlock(&ps_lock);
  109. }
  110. /* Must be called under dev_state->lock */
  111. static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
  112. int pasid, bool alloc)
  113. {
  114. struct pasid_state **root, **ptr;
  115. int level, index;
  116. level = dev_state->pasid_levels;
  117. root = dev_state->states;
  118. while (true) {
  119. index = (pasid >> (9 * level)) & 0x1ff;
  120. ptr = &root[index];
  121. if (level == 0)
  122. break;
  123. if (*ptr == NULL) {
  124. if (!alloc)
  125. return NULL;
  126. *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
  127. if (*ptr == NULL)
  128. return NULL;
  129. }
  130. root = (struct pasid_state **)*ptr;
  131. level -= 1;
  132. }
  133. return ptr;
  134. }
  135. static int set_pasid_state(struct device_state *dev_state,
  136. struct pasid_state *pasid_state,
  137. int pasid)
  138. {
  139. struct pasid_state **ptr;
  140. unsigned long flags;
  141. int ret;
  142. spin_lock_irqsave(&dev_state->lock, flags);
  143. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  144. ret = -ENOMEM;
  145. if (ptr == NULL)
  146. goto out_unlock;
  147. ret = -ENOMEM;
  148. if (*ptr != NULL)
  149. goto out_unlock;
  150. *ptr = pasid_state;
  151. ret = 0;
  152. out_unlock:
  153. spin_unlock_irqrestore(&dev_state->lock, flags);
  154. return ret;
  155. }
  156. static void clear_pasid_state(struct device_state *dev_state, int pasid)
  157. {
  158. struct pasid_state **ptr;
  159. unsigned long flags;
  160. spin_lock_irqsave(&dev_state->lock, flags);
  161. ptr = __get_pasid_state_ptr(dev_state, pasid, true);
  162. if (ptr == NULL)
  163. goto out_unlock;
  164. *ptr = NULL;
  165. out_unlock:
  166. spin_unlock_irqrestore(&dev_state->lock, flags);
  167. }
  168. static struct pasid_state *get_pasid_state(struct device_state *dev_state,
  169. int pasid)
  170. {
  171. struct pasid_state **ptr, *ret = NULL;
  172. unsigned long flags;
  173. spin_lock_irqsave(&dev_state->lock, flags);
  174. ptr = __get_pasid_state_ptr(dev_state, pasid, false);
  175. if (ptr == NULL)
  176. goto out_unlock;
  177. ret = *ptr;
  178. if (ret)
  179. atomic_inc(&ret->count);
  180. out_unlock:
  181. spin_unlock_irqrestore(&dev_state->lock, flags);
  182. return ret;
  183. }
  184. static void free_pasid_state(struct pasid_state *pasid_state)
  185. {
  186. kfree(pasid_state);
  187. }
  188. static void put_pasid_state(struct pasid_state *pasid_state)
  189. {
  190. if (atomic_dec_and_test(&pasid_state->count)) {
  191. put_device_state(pasid_state->device_state);
  192. mmput(pasid_state->mm);
  193. free_pasid_state(pasid_state);
  194. }
  195. }
  196. static void unbind_pasid(struct device_state *dev_state, int pasid)
  197. {
  198. struct pasid_state *pasid_state;
  199. pasid_state = get_pasid_state(dev_state, pasid);
  200. if (pasid_state == NULL)
  201. return;
  202. unlink_pasid_state(pasid_state);
  203. amd_iommu_domain_clear_gcr3(dev_state->domain, pasid);
  204. clear_pasid_state(dev_state, pasid);
  205. put_pasid_state(pasid_state); /* Reference taken in this function */
  206. put_pasid_state(pasid_state); /* Reference taken in bind() function */
  207. }
  208. static void free_pasid_states_level1(struct pasid_state **tbl)
  209. {
  210. int i;
  211. for (i = 0; i < 512; ++i) {
  212. if (tbl[i] == NULL)
  213. continue;
  214. free_page((unsigned long)tbl[i]);
  215. }
  216. }
  217. static void free_pasid_states_level2(struct pasid_state **tbl)
  218. {
  219. struct pasid_state **ptr;
  220. int i;
  221. for (i = 0; i < 512; ++i) {
  222. if (tbl[i] == NULL)
  223. continue;
  224. ptr = (struct pasid_state **)tbl[i];
  225. free_pasid_states_level1(ptr);
  226. }
  227. }
  228. static void free_pasid_states(struct device_state *dev_state)
  229. {
  230. struct pasid_state *pasid_state;
  231. int i;
  232. for (i = 0; i < dev_state->max_pasids; ++i) {
  233. pasid_state = get_pasid_state(dev_state, i);
  234. if (pasid_state == NULL)
  235. continue;
  236. unbind_pasid(dev_state, i);
  237. put_pasid_state(pasid_state);
  238. }
  239. if (dev_state->pasid_levels == 2)
  240. free_pasid_states_level2(dev_state->states);
  241. else if (dev_state->pasid_levels == 1)
  242. free_pasid_states_level1(dev_state->states);
  243. else if (dev_state->pasid_levels != 0)
  244. BUG();
  245. free_page((unsigned long)dev_state->states);
  246. }
  247. int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
  248. struct task_struct *task)
  249. {
  250. struct pasid_state *pasid_state;
  251. struct device_state *dev_state;
  252. u16 devid;
  253. int ret;
  254. might_sleep();
  255. if (!amd_iommu_v2_supported())
  256. return -ENODEV;
  257. devid = device_id(pdev);
  258. dev_state = get_device_state(devid);
  259. if (dev_state == NULL)
  260. return -EINVAL;
  261. ret = -EINVAL;
  262. if (pasid < 0 || pasid >= dev_state->max_pasids)
  263. goto out;
  264. ret = -ENOMEM;
  265. pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
  266. if (pasid_state == NULL)
  267. goto out;
  268. atomic_set(&pasid_state->count, 1);
  269. pasid_state->task = task;
  270. pasid_state->mm = get_task_mm(task);
  271. pasid_state->device_state = dev_state;
  272. pasid_state->pasid = pasid;
  273. if (pasid_state->mm == NULL)
  274. goto out_free;
  275. ret = set_pasid_state(dev_state, pasid_state, pasid);
  276. if (ret)
  277. goto out_free;
  278. ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
  279. __pa(pasid_state->mm->pgd));
  280. if (ret)
  281. goto out_clear_state;
  282. link_pasid_state(pasid_state);
  283. return 0;
  284. out_clear_state:
  285. clear_pasid_state(dev_state, pasid);
  286. out_free:
  287. put_pasid_state(pasid_state);
  288. out:
  289. put_device_state(dev_state);
  290. return ret;
  291. }
  292. EXPORT_SYMBOL(amd_iommu_bind_pasid);
  293. void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
  294. {
  295. struct device_state *dev_state;
  296. u16 devid;
  297. might_sleep();
  298. if (!amd_iommu_v2_supported())
  299. return;
  300. devid = device_id(pdev);
  301. dev_state = get_device_state(devid);
  302. if (dev_state == NULL)
  303. return;
  304. if (pasid < 0 || pasid >= dev_state->max_pasids)
  305. goto out;
  306. unbind_pasid(dev_state, pasid);
  307. out:
  308. put_device_state(dev_state);
  309. }
  310. EXPORT_SYMBOL(amd_iommu_unbind_pasid);
  311. int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
  312. {
  313. struct device_state *dev_state;
  314. unsigned long flags;
  315. int ret, tmp;
  316. u16 devid;
  317. might_sleep();
  318. if (!amd_iommu_v2_supported())
  319. return -ENODEV;
  320. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  321. return -EINVAL;
  322. devid = device_id(pdev);
  323. dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
  324. if (dev_state == NULL)
  325. return -ENOMEM;
  326. spin_lock_init(&dev_state->lock);
  327. dev_state->pdev = pdev;
  328. tmp = pasids;
  329. for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
  330. dev_state->pasid_levels += 1;
  331. atomic_set(&dev_state->count, 1);
  332. dev_state->max_pasids = pasids;
  333. ret = -ENOMEM;
  334. dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
  335. if (dev_state->states == NULL)
  336. goto out_free_dev_state;
  337. dev_state->domain = iommu_domain_alloc(&pci_bus_type);
  338. if (dev_state->domain == NULL)
  339. goto out_free_states;
  340. amd_iommu_domain_direct_map(dev_state->domain);
  341. ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
  342. if (ret)
  343. goto out_free_domain;
  344. ret = iommu_attach_device(dev_state->domain, &pdev->dev);
  345. if (ret != 0)
  346. goto out_free_domain;
  347. spin_lock_irqsave(&state_lock, flags);
  348. if (state_table[devid] != NULL) {
  349. spin_unlock_irqrestore(&state_lock, flags);
  350. ret = -EBUSY;
  351. goto out_free_domain;
  352. }
  353. state_table[devid] = dev_state;
  354. spin_unlock_irqrestore(&state_lock, flags);
  355. return 0;
  356. out_free_domain:
  357. iommu_domain_free(dev_state->domain);
  358. out_free_states:
  359. free_page((unsigned long)dev_state->states);
  360. out_free_dev_state:
  361. kfree(dev_state);
  362. return ret;
  363. }
  364. EXPORT_SYMBOL(amd_iommu_init_device);
  365. void amd_iommu_free_device(struct pci_dev *pdev)
  366. {
  367. struct device_state *dev_state;
  368. unsigned long flags;
  369. u16 devid;
  370. if (!amd_iommu_v2_supported())
  371. return;
  372. devid = device_id(pdev);
  373. spin_lock_irqsave(&state_lock, flags);
  374. dev_state = state_table[devid];
  375. if (dev_state == NULL) {
  376. spin_unlock_irqrestore(&state_lock, flags);
  377. return;
  378. }
  379. state_table[devid] = NULL;
  380. spin_unlock_irqrestore(&state_lock, flags);
  381. /* Get rid of any remaining pasid states */
  382. free_pasid_states(dev_state);
  383. put_device_state(dev_state);
  384. }
  385. EXPORT_SYMBOL(amd_iommu_free_device);
  386. static int __init amd_iommu_v2_init(void)
  387. {
  388. size_t state_table_size;
  389. pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>");
  390. spin_lock_init(&state_lock);
  391. state_table_size = MAX_DEVICES * sizeof(struct device_state *);
  392. state_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  393. get_order(state_table_size));
  394. if (state_table == NULL)
  395. return -ENOMEM;
  396. return 0;
  397. }
  398. static void __exit amd_iommu_v2_exit(void)
  399. {
  400. struct device_state *dev_state;
  401. size_t state_table_size;
  402. int i;
  403. for (i = 0; i < MAX_DEVICES; ++i) {
  404. dev_state = get_device_state(i);
  405. if (dev_state == NULL)
  406. continue;
  407. WARN_ON_ONCE(1);
  408. amd_iommu_free_device(dev_state->pdev);
  409. put_device_state(dev_state);
  410. }
  411. state_table_size = MAX_DEVICES * sizeof(struct device_state *);
  412. free_pages((unsigned long)state_table, get_order(state_table_size));
  413. }
  414. module_init(amd_iommu_v2_init);
  415. module_exit(amd_iommu_v2_exit);