qib_user_pages.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/mm.h>
  34. #include <linux/device.h>
  35. #include "qib.h"
  36. static void __qib_release_user_pages(struct page **p, size_t num_pages,
  37. int dirty)
  38. {
  39. size_t i;
  40. for (i = 0; i < num_pages; i++) {
  41. if (dirty)
  42. set_page_dirty_lock(p[i]);
  43. put_page(p[i]);
  44. }
  45. }
  46. /*
  47. * Call with current->mm->mmap_sem held.
  48. */
  49. static int __qib_get_user_pages(unsigned long start_page, size_t num_pages,
  50. struct page **p, struct vm_area_struct **vma)
  51. {
  52. unsigned long lock_limit;
  53. size_t got;
  54. int ret;
  55. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  56. if (num_pages > lock_limit && !capable(CAP_IPC_LOCK)) {
  57. ret = -ENOMEM;
  58. goto bail;
  59. }
  60. for (got = 0; got < num_pages; got += ret) {
  61. ret = get_user_pages(current, current->mm,
  62. start_page + got * PAGE_SIZE,
  63. num_pages - got, 1, 1,
  64. p + got, vma);
  65. if (ret < 0)
  66. goto bail_release;
  67. }
  68. current->mm->pinned_vm += num_pages;
  69. ret = 0;
  70. goto bail;
  71. bail_release:
  72. __qib_release_user_pages(p, got, 0);
  73. bail:
  74. return ret;
  75. }
  76. /**
  77. * qib_map_page - a safety wrapper around pci_map_page()
  78. *
  79. * A dma_addr of all 0's is interpreted by the chip as "disabled".
  80. * Unfortunately, it can also be a valid dma_addr returned on some
  81. * architectures.
  82. *
  83. * The powerpc iommu assigns dma_addrs in ascending order, so we don't
  84. * have to bother with retries or mapping a dummy page to insure we
  85. * don't just get the same mapping again.
  86. *
  87. * I'm sure we won't be so lucky with other iommu's, so FIXME.
  88. */
  89. dma_addr_t qib_map_page(struct pci_dev *hwdev, struct page *page,
  90. unsigned long offset, size_t size, int direction)
  91. {
  92. dma_addr_t phys;
  93. phys = pci_map_page(hwdev, page, offset, size, direction);
  94. if (phys == 0) {
  95. pci_unmap_page(hwdev, phys, size, direction);
  96. phys = pci_map_page(hwdev, page, offset, size, direction);
  97. /*
  98. * FIXME: If we get 0 again, we should keep this page,
  99. * map another, then free the 0 page.
  100. */
  101. }
  102. return phys;
  103. }
  104. /**
  105. * qib_get_user_pages - lock user pages into memory
  106. * @start_page: the start page
  107. * @num_pages: the number of pages
  108. * @p: the output page structures
  109. *
  110. * This function takes a given start page (page aligned user virtual
  111. * address) and pins it and the following specified number of pages. For
  112. * now, num_pages is always 1, but that will probably change at some point
  113. * (because caller is doing expected sends on a single virtually contiguous
  114. * buffer, so we can do all pages at once).
  115. */
  116. int qib_get_user_pages(unsigned long start_page, size_t num_pages,
  117. struct page **p)
  118. {
  119. int ret;
  120. down_write(&current->mm->mmap_sem);
  121. ret = __qib_get_user_pages(start_page, num_pages, p, NULL);
  122. up_write(&current->mm->mmap_sem);
  123. return ret;
  124. }
  125. void qib_release_user_pages(struct page **p, size_t num_pages)
  126. {
  127. if (current->mm) /* during close after signal, mm can be NULL */
  128. down_write(&current->mm->mmap_sem);
  129. __qib_release_user_pages(p, num_pages, 1);
  130. if (current->mm) {
  131. current->mm->pinned_vm -= num_pages;
  132. up_write(&current->mm->mmap_sem);
  133. }
  134. }