ttm_bo_vm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #include <ttm/ttm_module.h>
  31. #include <ttm/ttm_bo_driver.h>
  32. #include <ttm/ttm_placement.h>
  33. #include <linux/mm.h>
  34. #include <linux/version.h>
  35. #include <linux/rbtree.h>
  36. #include <linux/module.h>
  37. #include <linux/uaccess.h>
  38. #define TTM_BO_VM_NUM_PREFAULT 16
  39. static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
  40. unsigned long page_start,
  41. unsigned long num_pages)
  42. {
  43. struct rb_node *cur = bdev->addr_space_rb.rb_node;
  44. unsigned long cur_offset;
  45. struct ttm_buffer_object *bo;
  46. struct ttm_buffer_object *best_bo = NULL;
  47. while (likely(cur != NULL)) {
  48. bo = rb_entry(cur, struct ttm_buffer_object, vm_rb);
  49. cur_offset = bo->vm_node->start;
  50. if (page_start >= cur_offset) {
  51. cur = cur->rb_right;
  52. best_bo = bo;
  53. if (page_start == cur_offset)
  54. break;
  55. } else
  56. cur = cur->rb_left;
  57. }
  58. if (unlikely(best_bo == NULL))
  59. return NULL;
  60. if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
  61. (page_start + num_pages)))
  62. return NULL;
  63. return best_bo;
  64. }
  65. static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  66. {
  67. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
  68. vma->vm_private_data;
  69. struct ttm_bo_device *bdev = bo->bdev;
  70. unsigned long bus_base;
  71. unsigned long bus_offset;
  72. unsigned long bus_size;
  73. unsigned long page_offset;
  74. unsigned long page_last;
  75. unsigned long pfn;
  76. struct ttm_tt *ttm = NULL;
  77. struct page *page;
  78. int ret;
  79. int i;
  80. bool is_iomem;
  81. unsigned long address = (unsigned long)vmf->virtual_address;
  82. int retval = VM_FAULT_NOPAGE;
  83. /*
  84. * Work around locking order reversal in fault / nopfn
  85. * between mmap_sem and bo_reserve: Perform a trylock operation
  86. * for reserve, and if it fails, retry the fault after scheduling.
  87. */
  88. ret = ttm_bo_reserve(bo, true, true, false, 0);
  89. if (unlikely(ret != 0)) {
  90. if (ret == -EBUSY)
  91. set_need_resched();
  92. return VM_FAULT_NOPAGE;
  93. }
  94. /*
  95. * Wait for buffer data in transit, due to a pipelined
  96. * move.
  97. */
  98. spin_lock(&bo->lock);
  99. if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
  100. ret = ttm_bo_wait(bo, false, true, false);
  101. spin_unlock(&bo->lock);
  102. if (unlikely(ret != 0)) {
  103. retval = (ret != -ERESTART) ?
  104. VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
  105. goto out_unlock;
  106. }
  107. } else
  108. spin_unlock(&bo->lock);
  109. ret = ttm_bo_pci_offset(bdev, &bo->mem, &bus_base, &bus_offset,
  110. &bus_size);
  111. if (unlikely(ret != 0)) {
  112. retval = VM_FAULT_SIGBUS;
  113. goto out_unlock;
  114. }
  115. is_iomem = (bus_size != 0);
  116. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  117. bo->vm_node->start - vma->vm_pgoff;
  118. page_last = ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) +
  119. bo->vm_node->start - vma->vm_pgoff;
  120. if (unlikely(page_offset >= bo->num_pages)) {
  121. retval = VM_FAULT_SIGBUS;
  122. goto out_unlock;
  123. }
  124. /*
  125. * Strictly, we're not allowed to modify vma->vm_page_prot here,
  126. * since the mmap_sem is only held in read mode. However, we
  127. * modify only the caching bits of vma->vm_page_prot and
  128. * consider those bits protected by
  129. * the bo->mutex, as we should be the only writers.
  130. * There shouldn't really be any readers of these bits except
  131. * within vm_insert_mixed()? fork?
  132. *
  133. * TODO: Add a list of vmas to the bo, and change the
  134. * vma->vm_page_prot when the object changes caching policy, with
  135. * the correct locks held.
  136. */
  137. if (is_iomem) {
  138. vma->vm_page_prot = ttm_io_prot(bo->mem.placement,
  139. vma->vm_page_prot);
  140. } else {
  141. ttm = bo->ttm;
  142. vma->vm_page_prot = (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
  143. vm_get_page_prot(vma->vm_flags) :
  144. ttm_io_prot(bo->mem.placement, vma->vm_page_prot);
  145. }
  146. /*
  147. * Speculatively prefault a number of pages. Only error on
  148. * first page.
  149. */
  150. for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
  151. if (is_iomem)
  152. pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) +
  153. page_offset;
  154. else {
  155. page = ttm_tt_get_page(ttm, page_offset);
  156. if (unlikely(!page && i == 0)) {
  157. retval = VM_FAULT_OOM;
  158. goto out_unlock;
  159. } else if (unlikely(!page)) {
  160. break;
  161. }
  162. pfn = page_to_pfn(page);
  163. }
  164. ret = vm_insert_mixed(vma, address, pfn);
  165. /*
  166. * Somebody beat us to this PTE or prefaulting to
  167. * an already populated PTE, or prefaulting error.
  168. */
  169. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  170. break;
  171. else if (unlikely(ret != 0)) {
  172. retval =
  173. (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  174. goto out_unlock;
  175. }
  176. address += PAGE_SIZE;
  177. if (unlikely(++page_offset >= page_last))
  178. break;
  179. }
  180. out_unlock:
  181. ttm_bo_unreserve(bo);
  182. return retval;
  183. }
  184. static void ttm_bo_vm_open(struct vm_area_struct *vma)
  185. {
  186. struct ttm_buffer_object *bo =
  187. (struct ttm_buffer_object *)vma->vm_private_data;
  188. (void)ttm_bo_reference(bo);
  189. }
  190. static void ttm_bo_vm_close(struct vm_area_struct *vma)
  191. {
  192. struct ttm_buffer_object *bo =
  193. (struct ttm_buffer_object *)vma->vm_private_data;
  194. ttm_bo_unref(&bo);
  195. vma->vm_private_data = NULL;
  196. }
  197. static struct vm_operations_struct ttm_bo_vm_ops = {
  198. .fault = ttm_bo_vm_fault,
  199. .open = ttm_bo_vm_open,
  200. .close = ttm_bo_vm_close
  201. };
  202. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  203. struct ttm_bo_device *bdev)
  204. {
  205. struct ttm_bo_driver *driver;
  206. struct ttm_buffer_object *bo;
  207. int ret;
  208. read_lock(&bdev->vm_lock);
  209. bo = ttm_bo_vm_lookup_rb(bdev, vma->vm_pgoff,
  210. (vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  211. if (likely(bo != NULL))
  212. ttm_bo_reference(bo);
  213. read_unlock(&bdev->vm_lock);
  214. if (unlikely(bo == NULL)) {
  215. printk(KERN_ERR TTM_PFX
  216. "Could not find buffer object to map.\n");
  217. return -EINVAL;
  218. }
  219. driver = bo->bdev->driver;
  220. if (unlikely(!driver->verify_access)) {
  221. ret = -EPERM;
  222. goto out_unref;
  223. }
  224. ret = driver->verify_access(bo, filp);
  225. if (unlikely(ret != 0))
  226. goto out_unref;
  227. vma->vm_ops = &ttm_bo_vm_ops;
  228. /*
  229. * Note: We're transferring the bo reference to
  230. * vma->vm_private_data here.
  231. */
  232. vma->vm_private_data = bo;
  233. vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
  234. return 0;
  235. out_unref:
  236. ttm_bo_unref(&bo);
  237. return ret;
  238. }
  239. EXPORT_SYMBOL(ttm_bo_mmap);
  240. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  241. {
  242. if (vma->vm_pgoff != 0)
  243. return -EACCES;
  244. vma->vm_ops = &ttm_bo_vm_ops;
  245. vma->vm_private_data = ttm_bo_reference(bo);
  246. vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(ttm_fbdev_mmap);
  250. ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
  251. const char __user *wbuf, char __user *rbuf, size_t count,
  252. loff_t *f_pos, bool write)
  253. {
  254. struct ttm_buffer_object *bo;
  255. struct ttm_bo_driver *driver;
  256. struct ttm_bo_kmap_obj map;
  257. unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
  258. unsigned long kmap_offset;
  259. unsigned long kmap_end;
  260. unsigned long kmap_num;
  261. size_t io_size;
  262. unsigned int page_offset;
  263. char *virtual;
  264. int ret;
  265. bool no_wait = false;
  266. bool dummy;
  267. read_lock(&bdev->vm_lock);
  268. bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
  269. if (likely(bo != NULL))
  270. ttm_bo_reference(bo);
  271. read_unlock(&bdev->vm_lock);
  272. if (unlikely(bo == NULL))
  273. return -EFAULT;
  274. driver = bo->bdev->driver;
  275. if (unlikely(driver->verify_access)) {
  276. ret = -EPERM;
  277. goto out_unref;
  278. }
  279. ret = driver->verify_access(bo, filp);
  280. if (unlikely(ret != 0))
  281. goto out_unref;
  282. kmap_offset = dev_offset - bo->vm_node->start;
  283. if (unlikely(kmap_offset) >= bo->num_pages) {
  284. ret = -EFBIG;
  285. goto out_unref;
  286. }
  287. page_offset = *f_pos & ~PAGE_MASK;
  288. io_size = bo->num_pages - kmap_offset;
  289. io_size = (io_size << PAGE_SHIFT) - page_offset;
  290. if (count < io_size)
  291. io_size = count;
  292. kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
  293. kmap_num = kmap_end - kmap_offset + 1;
  294. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  295. switch (ret) {
  296. case 0:
  297. break;
  298. case -ERESTART:
  299. ret = -EINTR;
  300. goto out_unref;
  301. case -EBUSY:
  302. ret = -EAGAIN;
  303. goto out_unref;
  304. default:
  305. goto out_unref;
  306. }
  307. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  308. if (unlikely(ret != 0)) {
  309. ttm_bo_unreserve(bo);
  310. goto out_unref;
  311. }
  312. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  313. virtual += page_offset;
  314. if (write)
  315. ret = copy_from_user(virtual, wbuf, io_size);
  316. else
  317. ret = copy_to_user(rbuf, virtual, io_size);
  318. ttm_bo_kunmap(&map);
  319. ttm_bo_unreserve(bo);
  320. ttm_bo_unref(&bo);
  321. if (unlikely(ret != 0))
  322. return -EFBIG;
  323. *f_pos += io_size;
  324. return io_size;
  325. out_unref:
  326. ttm_bo_unref(&bo);
  327. return ret;
  328. }
  329. ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
  330. char __user *rbuf, size_t count, loff_t *f_pos,
  331. bool write)
  332. {
  333. struct ttm_bo_kmap_obj map;
  334. unsigned long kmap_offset;
  335. unsigned long kmap_end;
  336. unsigned long kmap_num;
  337. size_t io_size;
  338. unsigned int page_offset;
  339. char *virtual;
  340. int ret;
  341. bool no_wait = false;
  342. bool dummy;
  343. kmap_offset = (*f_pos >> PAGE_SHIFT);
  344. if (unlikely(kmap_offset) >= bo->num_pages)
  345. return -EFBIG;
  346. page_offset = *f_pos & ~PAGE_MASK;
  347. io_size = bo->num_pages - kmap_offset;
  348. io_size = (io_size << PAGE_SHIFT) - page_offset;
  349. if (count < io_size)
  350. io_size = count;
  351. kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
  352. kmap_num = kmap_end - kmap_offset + 1;
  353. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  354. switch (ret) {
  355. case 0:
  356. break;
  357. case -ERESTART:
  358. return -EINTR;
  359. case -EBUSY:
  360. return -EAGAIN;
  361. default:
  362. return ret;
  363. }
  364. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  365. if (unlikely(ret != 0)) {
  366. ttm_bo_unreserve(bo);
  367. return ret;
  368. }
  369. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  370. virtual += page_offset;
  371. if (write)
  372. ret = copy_from_user(virtual, wbuf, io_size);
  373. else
  374. ret = copy_to_user(rbuf, virtual, io_size);
  375. ttm_bo_kunmap(&map);
  376. ttm_bo_unreserve(bo);
  377. ttm_bo_unref(&bo);
  378. if (unlikely(ret != 0))
  379. return ret;
  380. *f_pos += io_size;
  381. return io_size;
  382. }