usnic_uiom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2013 Cisco Systems. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/mm.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/sched.h>
  37. #include <linux/hugetlb.h>
  38. #include <linux/dma-attrs.h>
  39. #include <linux/iommu.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/list.h>
  42. #include <linux/pci.h>
  43. #include "usnic_log.h"
  44. #include "usnic_uiom.h"
  45. #include "usnic_uiom_interval_tree.h"
  46. static struct workqueue_struct *usnic_uiom_wq;
  47. #define USNIC_UIOM_PAGE_CHUNK \
  48. ((PAGE_SIZE - offsetof(struct usnic_uiom_chunk, page_list)) /\
  49. ((void *) &((struct usnic_uiom_chunk *) 0)->page_list[1] - \
  50. (void *) &((struct usnic_uiom_chunk *) 0)->page_list[0]))
  51. static void usnic_uiom_reg_account(struct work_struct *work)
  52. {
  53. struct usnic_uiom_reg *umem = container_of(work,
  54. struct usnic_uiom_reg, work);
  55. down_write(&umem->mm->mmap_sem);
  56. umem->mm->locked_vm -= umem->diff;
  57. up_write(&umem->mm->mmap_sem);
  58. mmput(umem->mm);
  59. kfree(umem);
  60. }
  61. static int usnic_uiom_dma_fault(struct iommu_domain *domain,
  62. struct device *dev,
  63. unsigned long iova, int flags,
  64. void *token)
  65. {
  66. usnic_err("Device %s iommu fault domain 0x%pK va 0x%lx flags 0x%x\n",
  67. dev_name(dev),
  68. domain, iova, flags);
  69. return -ENOSYS;
  70. }
  71. static void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty)
  72. {
  73. struct usnic_uiom_chunk *chunk, *tmp;
  74. struct page *page;
  75. struct scatterlist *sg;
  76. int i;
  77. dma_addr_t pa;
  78. list_for_each_entry_safe(chunk, tmp, chunk_list, list) {
  79. for_each_sg(chunk->page_list, sg, chunk->nents, i) {
  80. page = sg_page(sg);
  81. pa = sg_phys(sg);
  82. if (dirty)
  83. set_page_dirty_lock(page);
  84. put_page(page);
  85. usnic_dbg("pa: %pa\n", &pa);
  86. }
  87. kfree(chunk);
  88. }
  89. }
  90. static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable,
  91. int dmasync, struct list_head *chunk_list)
  92. {
  93. struct page **page_list;
  94. struct scatterlist *sg;
  95. struct usnic_uiom_chunk *chunk;
  96. unsigned long locked;
  97. unsigned long lock_limit;
  98. unsigned long cur_base;
  99. unsigned long npages;
  100. int ret;
  101. int off;
  102. int i;
  103. int flags;
  104. dma_addr_t pa;
  105. DEFINE_DMA_ATTRS(attrs);
  106. if (dmasync)
  107. dma_set_attr(DMA_ATTR_WRITE_BARRIER, &attrs);
  108. if (!can_do_mlock())
  109. return -EPERM;
  110. INIT_LIST_HEAD(chunk_list);
  111. page_list = (struct page **) __get_free_page(GFP_KERNEL);
  112. if (!page_list)
  113. return -ENOMEM;
  114. npages = PAGE_ALIGN(size + (addr & ~PAGE_MASK)) >> PAGE_SHIFT;
  115. down_write(&current->mm->mmap_sem);
  116. locked = npages + current->mm->locked_vm;
  117. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  118. if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
  119. ret = -ENOMEM;
  120. goto out;
  121. }
  122. flags = IOMMU_READ | IOMMU_CACHE;
  123. flags |= (writable) ? IOMMU_WRITE : 0;
  124. cur_base = addr & PAGE_MASK;
  125. ret = 0;
  126. while (npages) {
  127. ret = get_user_pages(current, current->mm, cur_base,
  128. min_t(unsigned long, npages,
  129. PAGE_SIZE / sizeof(struct page *)),
  130. 1, !writable, page_list, NULL);
  131. if (ret < 0)
  132. goto out;
  133. npages -= ret;
  134. off = 0;
  135. while (ret) {
  136. chunk = kmalloc(sizeof(*chunk) +
  137. sizeof(struct scatterlist) *
  138. min_t(int, ret, USNIC_UIOM_PAGE_CHUNK),
  139. GFP_KERNEL);
  140. if (!chunk) {
  141. ret = -ENOMEM;
  142. goto out;
  143. }
  144. chunk->nents = min_t(int, ret, USNIC_UIOM_PAGE_CHUNK);
  145. sg_init_table(chunk->page_list, chunk->nents);
  146. for_each_sg(chunk->page_list, sg, chunk->nents, i) {
  147. sg_set_page(sg, page_list[i + off],
  148. PAGE_SIZE, 0);
  149. pa = sg_phys(sg);
  150. usnic_dbg("va: 0x%lx pa: %pa\n",
  151. cur_base + i*PAGE_SIZE, &pa);
  152. }
  153. cur_base += chunk->nents * PAGE_SIZE;
  154. ret -= chunk->nents;
  155. off += chunk->nents;
  156. list_add_tail(&chunk->list, chunk_list);
  157. }
  158. ret = 0;
  159. }
  160. out:
  161. if (ret < 0)
  162. usnic_uiom_put_pages(chunk_list, 0);
  163. else
  164. current->mm->locked_vm = locked;
  165. up_write(&current->mm->mmap_sem);
  166. free_page((unsigned long) page_list);
  167. return ret;
  168. }
  169. static void usnic_uiom_unmap_sorted_intervals(struct list_head *intervals,
  170. struct usnic_uiom_pd *pd)
  171. {
  172. struct usnic_uiom_interval_node *interval, *tmp;
  173. long unsigned va, size;
  174. list_for_each_entry_safe(interval, tmp, intervals, link) {
  175. va = interval->start << PAGE_SHIFT;
  176. size = ((interval->last - interval->start) + 1) << PAGE_SHIFT;
  177. while (size > 0) {
  178. /* Workaround for RH 970401 */
  179. usnic_dbg("va 0x%lx size 0x%lx", va, PAGE_SIZE);
  180. iommu_unmap(pd->domain, va, PAGE_SIZE);
  181. va += PAGE_SIZE;
  182. size -= PAGE_SIZE;
  183. }
  184. }
  185. }
  186. static void __usnic_uiom_reg_release(struct usnic_uiom_pd *pd,
  187. struct usnic_uiom_reg *uiomr,
  188. int dirty)
  189. {
  190. int npages;
  191. unsigned long vpn_start, vpn_last;
  192. struct usnic_uiom_interval_node *interval, *tmp;
  193. int writable = 0;
  194. LIST_HEAD(rm_intervals);
  195. npages = PAGE_ALIGN(uiomr->length + uiomr->offset) >> PAGE_SHIFT;
  196. vpn_start = (uiomr->va & PAGE_MASK) >> PAGE_SHIFT;
  197. vpn_last = vpn_start + npages - 1;
  198. spin_lock(&pd->lock);
  199. usnic_uiom_remove_interval(&pd->rb_root, vpn_start,
  200. vpn_last, &rm_intervals);
  201. usnic_uiom_unmap_sorted_intervals(&rm_intervals, pd);
  202. list_for_each_entry_safe(interval, tmp, &rm_intervals, link) {
  203. if (interval->flags & IOMMU_WRITE)
  204. writable = 1;
  205. list_del(&interval->link);
  206. kfree(interval);
  207. }
  208. usnic_uiom_put_pages(&uiomr->chunk_list, dirty & writable);
  209. spin_unlock(&pd->lock);
  210. }
  211. static int usnic_uiom_map_sorted_intervals(struct list_head *intervals,
  212. struct usnic_uiom_reg *uiomr)
  213. {
  214. int i, err;
  215. size_t size;
  216. struct usnic_uiom_chunk *chunk;
  217. struct usnic_uiom_interval_node *interval_node;
  218. dma_addr_t pa;
  219. dma_addr_t pa_start = 0;
  220. dma_addr_t pa_end = 0;
  221. long int va_start = -EINVAL;
  222. struct usnic_uiom_pd *pd = uiomr->pd;
  223. long int va = uiomr->va & PAGE_MASK;
  224. int flags = IOMMU_READ | IOMMU_CACHE;
  225. flags |= (uiomr->writable) ? IOMMU_WRITE : 0;
  226. chunk = list_first_entry(&uiomr->chunk_list, struct usnic_uiom_chunk,
  227. list);
  228. list_for_each_entry(interval_node, intervals, link) {
  229. iter_chunk:
  230. for (i = 0; i < chunk->nents; i++, va += PAGE_SIZE) {
  231. pa = sg_phys(&chunk->page_list[i]);
  232. if ((va >> PAGE_SHIFT) < interval_node->start)
  233. continue;
  234. if ((va >> PAGE_SHIFT) == interval_node->start) {
  235. /* First page of the interval */
  236. va_start = va;
  237. pa_start = pa;
  238. pa_end = pa;
  239. }
  240. WARN_ON(va_start == -EINVAL);
  241. if ((pa_end + PAGE_SIZE != pa) &&
  242. (pa != pa_start)) {
  243. /* PAs are not contiguous */
  244. size = pa_end - pa_start + PAGE_SIZE;
  245. usnic_dbg("va 0x%lx pa %pa size 0x%zx flags 0x%x",
  246. va_start, &pa_start, size, flags);
  247. err = iommu_map(pd->domain, va_start, pa_start,
  248. size, flags);
  249. if (err) {
  250. usnic_err("Failed to map va 0x%lx pa %pa size 0x%zx with err %d\n",
  251. va_start, &pa_start, size, err);
  252. goto err_out;
  253. }
  254. va_start = va;
  255. pa_start = pa;
  256. pa_end = pa;
  257. }
  258. if ((va >> PAGE_SHIFT) == interval_node->last) {
  259. /* Last page of the interval */
  260. size = pa - pa_start + PAGE_SIZE;
  261. usnic_dbg("va 0x%lx pa %pa size 0x%zx flags 0x%x\n",
  262. va_start, &pa_start, size, flags);
  263. err = iommu_map(pd->domain, va_start, pa_start,
  264. size, flags);
  265. if (err) {
  266. usnic_err("Failed to map va 0x%lx pa %pa size 0x%zx with err %d\n",
  267. va_start, &pa_start, size, err);
  268. goto err_out;
  269. }
  270. break;
  271. }
  272. if (pa != pa_start)
  273. pa_end += PAGE_SIZE;
  274. }
  275. if (i == chunk->nents) {
  276. /*
  277. * Hit last entry of the chunk,
  278. * hence advance to next chunk
  279. */
  280. chunk = list_first_entry(&chunk->list,
  281. struct usnic_uiom_chunk,
  282. list);
  283. goto iter_chunk;
  284. }
  285. }
  286. return 0;
  287. err_out:
  288. usnic_uiom_unmap_sorted_intervals(intervals, pd);
  289. return err;
  290. }
  291. struct usnic_uiom_reg *usnic_uiom_reg_get(struct usnic_uiom_pd *pd,
  292. unsigned long addr, size_t size,
  293. int writable, int dmasync)
  294. {
  295. struct usnic_uiom_reg *uiomr;
  296. unsigned long va_base, vpn_start, vpn_last;
  297. unsigned long npages;
  298. int offset, err;
  299. LIST_HEAD(sorted_diff_intervals);
  300. /*
  301. * Intel IOMMU map throws an error if a translation entry is
  302. * changed from read to write. This module may not unmap
  303. * and then remap the entry after fixing the permission
  304. * b/c this open up a small windows where hw DMA may page fault
  305. * Hence, make all entries to be writable.
  306. */
  307. writable = 1;
  308. va_base = addr & PAGE_MASK;
  309. offset = addr & ~PAGE_MASK;
  310. npages = PAGE_ALIGN(size + offset) >> PAGE_SHIFT;
  311. vpn_start = (addr & PAGE_MASK) >> PAGE_SHIFT;
  312. vpn_last = vpn_start + npages - 1;
  313. uiomr = kmalloc(sizeof(*uiomr), GFP_KERNEL);
  314. if (!uiomr)
  315. return ERR_PTR(-ENOMEM);
  316. uiomr->va = va_base;
  317. uiomr->offset = offset;
  318. uiomr->length = size;
  319. uiomr->writable = writable;
  320. uiomr->pd = pd;
  321. err = usnic_uiom_get_pages(addr, size, writable, dmasync,
  322. &uiomr->chunk_list);
  323. if (err) {
  324. usnic_err("Failed get_pages vpn [0x%lx,0x%lx] err %d\n",
  325. vpn_start, vpn_last, err);
  326. goto out_free_uiomr;
  327. }
  328. spin_lock(&pd->lock);
  329. err = usnic_uiom_get_intervals_diff(vpn_start, vpn_last,
  330. (writable) ? IOMMU_WRITE : 0,
  331. IOMMU_WRITE,
  332. &pd->rb_root,
  333. &sorted_diff_intervals);
  334. if (err) {
  335. usnic_err("Failed disjoint interval vpn [0x%lx,0x%lx] err %d\n",
  336. vpn_start, vpn_last, err);
  337. goto out_put_pages;
  338. }
  339. err = usnic_uiom_map_sorted_intervals(&sorted_diff_intervals, uiomr);
  340. if (err) {
  341. usnic_err("Failed map interval vpn [0x%lx,0x%lx] err %d\n",
  342. vpn_start, vpn_last, err);
  343. goto out_put_intervals;
  344. }
  345. err = usnic_uiom_insert_interval(&pd->rb_root, vpn_start, vpn_last,
  346. (writable) ? IOMMU_WRITE : 0);
  347. if (err) {
  348. usnic_err("Failed insert interval vpn [0x%lx,0x%lx] err %d\n",
  349. vpn_start, vpn_last, err);
  350. goto out_unmap_intervals;
  351. }
  352. usnic_uiom_put_interval_set(&sorted_diff_intervals);
  353. spin_unlock(&pd->lock);
  354. return uiomr;
  355. out_unmap_intervals:
  356. usnic_uiom_unmap_sorted_intervals(&sorted_diff_intervals, pd);
  357. out_put_intervals:
  358. usnic_uiom_put_interval_set(&sorted_diff_intervals);
  359. out_put_pages:
  360. usnic_uiom_put_pages(&uiomr->chunk_list, 0);
  361. spin_unlock(&pd->lock);
  362. out_free_uiomr:
  363. kfree(uiomr);
  364. return ERR_PTR(err);
  365. }
  366. void usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr, int closing)
  367. {
  368. struct mm_struct *mm;
  369. unsigned long diff;
  370. __usnic_uiom_reg_release(uiomr->pd, uiomr, 1);
  371. mm = get_task_mm(current);
  372. if (!mm) {
  373. kfree(uiomr);
  374. return;
  375. }
  376. diff = PAGE_ALIGN(uiomr->length + uiomr->offset) >> PAGE_SHIFT;
  377. /*
  378. * We may be called with the mm's mmap_sem already held. This
  379. * can happen when a userspace munmap() is the call that drops
  380. * the last reference to our file and calls our release
  381. * method. If there are memory regions to destroy, we'll end
  382. * up here and not be able to take the mmap_sem. In that case
  383. * we defer the vm_locked accounting to the system workqueue.
  384. */
  385. if (closing) {
  386. if (!down_write_trylock(&mm->mmap_sem)) {
  387. INIT_WORK(&uiomr->work, usnic_uiom_reg_account);
  388. uiomr->mm = mm;
  389. uiomr->diff = diff;
  390. queue_work(usnic_uiom_wq, &uiomr->work);
  391. return;
  392. }
  393. } else
  394. down_write(&mm->mmap_sem);
  395. current->mm->locked_vm -= diff;
  396. up_write(&mm->mmap_sem);
  397. mmput(mm);
  398. kfree(uiomr);
  399. }
  400. struct usnic_uiom_pd *usnic_uiom_alloc_pd(void)
  401. {
  402. struct usnic_uiom_pd *pd;
  403. void *domain;
  404. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  405. if (!pd)
  406. return ERR_PTR(-ENOMEM);
  407. pd->domain = domain = iommu_domain_alloc(&pci_bus_type);
  408. if (!domain) {
  409. usnic_err("Failed to allocate IOMMU domain");
  410. kfree(pd);
  411. return ERR_PTR(-ENOMEM);
  412. }
  413. iommu_set_fault_handler(pd->domain, usnic_uiom_dma_fault, NULL);
  414. spin_lock_init(&pd->lock);
  415. INIT_LIST_HEAD(&pd->devs);
  416. return pd;
  417. }
  418. void usnic_uiom_dealloc_pd(struct usnic_uiom_pd *pd)
  419. {
  420. iommu_domain_free(pd->domain);
  421. kfree(pd);
  422. }
  423. int usnic_uiom_attach_dev_to_pd(struct usnic_uiom_pd *pd, struct device *dev)
  424. {
  425. struct usnic_uiom_dev *uiom_dev;
  426. int err;
  427. uiom_dev = kzalloc(sizeof(*uiom_dev), GFP_ATOMIC);
  428. if (!uiom_dev)
  429. return -ENOMEM;
  430. uiom_dev->dev = dev;
  431. err = iommu_attach_device(pd->domain, dev);
  432. if (err)
  433. goto out_free_dev;
  434. if (!iommu_capable(dev->bus, IOMMU_CAP_CACHE_COHERENCY)) {
  435. usnic_err("IOMMU of %s does not support cache coherency\n",
  436. dev_name(dev));
  437. err = -EINVAL;
  438. goto out_detach_device;
  439. }
  440. spin_lock(&pd->lock);
  441. list_add_tail(&uiom_dev->link, &pd->devs);
  442. pd->dev_cnt++;
  443. spin_unlock(&pd->lock);
  444. return 0;
  445. out_detach_device:
  446. iommu_detach_device(pd->domain, dev);
  447. out_free_dev:
  448. kfree(uiom_dev);
  449. return err;
  450. }
  451. void usnic_uiom_detach_dev_from_pd(struct usnic_uiom_pd *pd, struct device *dev)
  452. {
  453. struct usnic_uiom_dev *uiom_dev;
  454. int found = 0;
  455. spin_lock(&pd->lock);
  456. list_for_each_entry(uiom_dev, &pd->devs, link) {
  457. if (uiom_dev->dev == dev) {
  458. found = 1;
  459. break;
  460. }
  461. }
  462. if (!found) {
  463. usnic_err("Unable to free dev %s - not found\n",
  464. dev_name(dev));
  465. spin_unlock(&pd->lock);
  466. return;
  467. }
  468. list_del(&uiom_dev->link);
  469. pd->dev_cnt--;
  470. spin_unlock(&pd->lock);
  471. return iommu_detach_device(pd->domain, dev);
  472. }
  473. struct device **usnic_uiom_get_dev_list(struct usnic_uiom_pd *pd)
  474. {
  475. struct usnic_uiom_dev *uiom_dev;
  476. struct device **devs;
  477. int i = 0;
  478. spin_lock(&pd->lock);
  479. devs = kcalloc(pd->dev_cnt + 1, sizeof(*devs), GFP_ATOMIC);
  480. if (!devs) {
  481. devs = ERR_PTR(-ENOMEM);
  482. goto out;
  483. }
  484. list_for_each_entry(uiom_dev, &pd->devs, link) {
  485. devs[i++] = uiom_dev->dev;
  486. }
  487. out:
  488. spin_unlock(&pd->lock);
  489. return devs;
  490. }
  491. void usnic_uiom_free_dev_list(struct device **devs)
  492. {
  493. kfree(devs);
  494. }
  495. int usnic_uiom_init(char *drv_name)
  496. {
  497. if (!iommu_present(&pci_bus_type)) {
  498. usnic_err("IOMMU required but not present or enabled. USNIC QPs will not function w/o enabling IOMMU\n");
  499. return -EPERM;
  500. }
  501. usnic_uiom_wq = create_workqueue(drv_name);
  502. if (!usnic_uiom_wq) {
  503. usnic_err("Unable to alloc wq for drv %s\n", drv_name);
  504. return -ENOMEM;
  505. }
  506. return 0;
  507. }
  508. void usnic_uiom_fini(void)
  509. {
  510. flush_workqueue(usnic_uiom_wq);
  511. destroy_workqueue(usnic_uiom_wq);
  512. }