ttm_bo_vm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/rbtree.h>
  35. #include <linux/module.h>
  36. #include <linux/uaccess.h>
  37. #define TTM_BO_VM_NUM_PREFAULT 16
  38. static struct ttm_buffer_object *ttm_bo_vm_lookup_rb(struct ttm_bo_device *bdev,
  39. unsigned long page_start,
  40. unsigned long num_pages)
  41. {
  42. struct rb_node *cur = bdev->addr_space_rb.rb_node;
  43. unsigned long cur_offset;
  44. struct ttm_buffer_object *bo;
  45. struct ttm_buffer_object *best_bo = NULL;
  46. while (likely(cur != NULL)) {
  47. bo = rb_entry(cur, struct ttm_buffer_object, vm_rb);
  48. cur_offset = bo->vm_node->start;
  49. if (page_start >= cur_offset) {
  50. cur = cur->rb_right;
  51. best_bo = bo;
  52. if (page_start == cur_offset)
  53. break;
  54. } else
  55. cur = cur->rb_left;
  56. }
  57. if (unlikely(best_bo == NULL))
  58. return NULL;
  59. if (unlikely((best_bo->vm_node->start + best_bo->num_pages) <
  60. (page_start + num_pages)))
  61. return NULL;
  62. return best_bo;
  63. }
  64. static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  65. {
  66. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
  67. vma->vm_private_data;
  68. struct ttm_bo_device *bdev = bo->bdev;
  69. unsigned long bus_base;
  70. unsigned long bus_offset;
  71. unsigned long bus_size;
  72. unsigned long page_offset;
  73. unsigned long page_last;
  74. unsigned long pfn;
  75. struct ttm_tt *ttm = NULL;
  76. struct page *page;
  77. int ret;
  78. int i;
  79. bool is_iomem;
  80. unsigned long address = (unsigned long)vmf->virtual_address;
  81. int retval = VM_FAULT_NOPAGE;
  82. /*
  83. * Work around locking order reversal in fault / nopfn
  84. * between mmap_sem and bo_reserve: Perform a trylock operation
  85. * for reserve, and if it fails, retry the fault after scheduling.
  86. */
  87. ret = ttm_bo_reserve(bo, true, true, false, 0);
  88. if (unlikely(ret != 0)) {
  89. if (ret == -EBUSY)
  90. set_need_resched();
  91. return VM_FAULT_NOPAGE;
  92. }
  93. if (bdev->driver->fault_reserve_notify)
  94. bdev->driver->fault_reserve_notify(bo);
  95. /*
  96. * Wait for buffer data in transit, due to a pipelined
  97. * move.
  98. */
  99. spin_lock(&bo->lock);
  100. if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
  101. ret = ttm_bo_wait(bo, false, true, false);
  102. spin_unlock(&bo->lock);
  103. if (unlikely(ret != 0)) {
  104. retval = (ret != -ERESTARTSYS) ?
  105. VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
  106. goto out_unlock;
  107. }
  108. } else
  109. spin_unlock(&bo->lock);
  110. ret = ttm_bo_pci_offset(bdev, &bo->mem, &bus_base, &bus_offset,
  111. &bus_size);
  112. if (unlikely(ret != 0)) {
  113. retval = VM_FAULT_SIGBUS;
  114. goto out_unlock;
  115. }
  116. is_iomem = (bus_size != 0);
  117. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  118. bo->vm_node->start - vma->vm_pgoff;
  119. page_last = ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) +
  120. bo->vm_node->start - vma->vm_pgoff;
  121. if (unlikely(page_offset >= bo->num_pages)) {
  122. retval = VM_FAULT_SIGBUS;
  123. goto out_unlock;
  124. }
  125. /*
  126. * Strictly, we're not allowed to modify vma->vm_page_prot here,
  127. * since the mmap_sem is only held in read mode. However, we
  128. * modify only the caching bits of vma->vm_page_prot and
  129. * consider those bits protected by
  130. * the bo->mutex, as we should be the only writers.
  131. * There shouldn't really be any readers of these bits except
  132. * within vm_insert_mixed()? fork?
  133. *
  134. * TODO: Add a list of vmas to the bo, and change the
  135. * vma->vm_page_prot when the object changes caching policy, with
  136. * the correct locks held.
  137. */
  138. if (is_iomem) {
  139. vma->vm_page_prot = ttm_io_prot(bo->mem.placement,
  140. vma->vm_page_prot);
  141. } else {
  142. ttm = bo->ttm;
  143. vma->vm_page_prot = (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
  144. vm_get_page_prot(vma->vm_flags) :
  145. ttm_io_prot(bo->mem.placement, vma->vm_page_prot);
  146. }
  147. /*
  148. * Speculatively prefault a number of pages. Only error on
  149. * first page.
  150. */
  151. for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
  152. if (is_iomem)
  153. pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) +
  154. page_offset;
  155. else {
  156. page = ttm_tt_get_page(ttm, page_offset);
  157. if (unlikely(!page && i == 0)) {
  158. retval = VM_FAULT_OOM;
  159. goto out_unlock;
  160. } else if (unlikely(!page)) {
  161. break;
  162. }
  163. pfn = page_to_pfn(page);
  164. }
  165. ret = vm_insert_mixed(vma, address, pfn);
  166. /*
  167. * Somebody beat us to this PTE or prefaulting to
  168. * an already populated PTE, or prefaulting error.
  169. */
  170. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  171. break;
  172. else if (unlikely(ret != 0)) {
  173. retval =
  174. (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  175. goto out_unlock;
  176. }
  177. address += PAGE_SIZE;
  178. if (unlikely(++page_offset >= page_last))
  179. break;
  180. }
  181. out_unlock:
  182. ttm_bo_unreserve(bo);
  183. return retval;
  184. }
  185. static void ttm_bo_vm_open(struct vm_area_struct *vma)
  186. {
  187. struct ttm_buffer_object *bo =
  188. (struct ttm_buffer_object *)vma->vm_private_data;
  189. (void)ttm_bo_reference(bo);
  190. }
  191. static void ttm_bo_vm_close(struct vm_area_struct *vma)
  192. {
  193. struct ttm_buffer_object *bo =
  194. (struct ttm_buffer_object *)vma->vm_private_data;
  195. ttm_bo_unref(&bo);
  196. vma->vm_private_data = NULL;
  197. }
  198. static const struct vm_operations_struct ttm_bo_vm_ops = {
  199. .fault = ttm_bo_vm_fault,
  200. .open = ttm_bo_vm_open,
  201. .close = ttm_bo_vm_close
  202. };
  203. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  204. struct ttm_bo_device *bdev)
  205. {
  206. struct ttm_bo_driver *driver;
  207. struct ttm_buffer_object *bo;
  208. int ret;
  209. read_lock(&bdev->vm_lock);
  210. bo = ttm_bo_vm_lookup_rb(bdev, vma->vm_pgoff,
  211. (vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
  212. if (likely(bo != NULL))
  213. ttm_bo_reference(bo);
  214. read_unlock(&bdev->vm_lock);
  215. if (unlikely(bo == NULL)) {
  216. printk(KERN_ERR TTM_PFX
  217. "Could not find buffer object to map.\n");
  218. return -EINVAL;
  219. }
  220. driver = bo->bdev->driver;
  221. if (unlikely(!driver->verify_access)) {
  222. ret = -EPERM;
  223. goto out_unref;
  224. }
  225. ret = driver->verify_access(bo, filp);
  226. if (unlikely(ret != 0))
  227. goto out_unref;
  228. vma->vm_ops = &ttm_bo_vm_ops;
  229. /*
  230. * Note: We're transferring the bo reference to
  231. * vma->vm_private_data here.
  232. */
  233. vma->vm_private_data = bo;
  234. vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
  235. return 0;
  236. out_unref:
  237. ttm_bo_unref(&bo);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL(ttm_bo_mmap);
  241. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  242. {
  243. if (vma->vm_pgoff != 0)
  244. return -EACCES;
  245. vma->vm_ops = &ttm_bo_vm_ops;
  246. vma->vm_private_data = ttm_bo_reference(bo);
  247. vma->vm_flags |= VM_RESERVED | VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(ttm_fbdev_mmap);
  251. ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
  252. const char __user *wbuf, char __user *rbuf, size_t count,
  253. loff_t *f_pos, bool write)
  254. {
  255. struct ttm_buffer_object *bo;
  256. struct ttm_bo_driver *driver;
  257. struct ttm_bo_kmap_obj map;
  258. unsigned long dev_offset = (*f_pos >> PAGE_SHIFT);
  259. unsigned long kmap_offset;
  260. unsigned long kmap_end;
  261. unsigned long kmap_num;
  262. size_t io_size;
  263. unsigned int page_offset;
  264. char *virtual;
  265. int ret;
  266. bool no_wait = false;
  267. bool dummy;
  268. read_lock(&bdev->vm_lock);
  269. bo = ttm_bo_vm_lookup_rb(bdev, dev_offset, 1);
  270. if (likely(bo != NULL))
  271. ttm_bo_reference(bo);
  272. read_unlock(&bdev->vm_lock);
  273. if (unlikely(bo == NULL))
  274. return -EFAULT;
  275. driver = bo->bdev->driver;
  276. if (unlikely(!driver->verify_access)) {
  277. ret = -EPERM;
  278. goto out_unref;
  279. }
  280. ret = driver->verify_access(bo, filp);
  281. if (unlikely(ret != 0))
  282. goto out_unref;
  283. kmap_offset = dev_offset - bo->vm_node->start;
  284. if (unlikely(kmap_offset >= bo->num_pages)) {
  285. ret = -EFBIG;
  286. goto out_unref;
  287. }
  288. page_offset = *f_pos & ~PAGE_MASK;
  289. io_size = bo->num_pages - kmap_offset;
  290. io_size = (io_size << PAGE_SHIFT) - page_offset;
  291. if (count < io_size)
  292. io_size = count;
  293. kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
  294. kmap_num = kmap_end - kmap_offset + 1;
  295. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  296. switch (ret) {
  297. case 0:
  298. break;
  299. case -EBUSY:
  300. ret = -EAGAIN;
  301. goto out_unref;
  302. default:
  303. goto out_unref;
  304. }
  305. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  306. if (unlikely(ret != 0)) {
  307. ttm_bo_unreserve(bo);
  308. goto out_unref;
  309. }
  310. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  311. virtual += page_offset;
  312. if (write)
  313. ret = copy_from_user(virtual, wbuf, io_size);
  314. else
  315. ret = copy_to_user(rbuf, virtual, io_size);
  316. ttm_bo_kunmap(&map);
  317. ttm_bo_unreserve(bo);
  318. ttm_bo_unref(&bo);
  319. if (unlikely(ret != 0))
  320. return -EFBIG;
  321. *f_pos += io_size;
  322. return io_size;
  323. out_unref:
  324. ttm_bo_unref(&bo);
  325. return ret;
  326. }
  327. ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf,
  328. char __user *rbuf, size_t count, loff_t *f_pos,
  329. bool write)
  330. {
  331. struct ttm_bo_kmap_obj map;
  332. unsigned long kmap_offset;
  333. unsigned long kmap_end;
  334. unsigned long kmap_num;
  335. size_t io_size;
  336. unsigned int page_offset;
  337. char *virtual;
  338. int ret;
  339. bool no_wait = false;
  340. bool dummy;
  341. kmap_offset = (*f_pos >> PAGE_SHIFT);
  342. if (unlikely(kmap_offset >= bo->num_pages))
  343. return -EFBIG;
  344. page_offset = *f_pos & ~PAGE_MASK;
  345. io_size = bo->num_pages - kmap_offset;
  346. io_size = (io_size << PAGE_SHIFT) - page_offset;
  347. if (count < io_size)
  348. io_size = count;
  349. kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT;
  350. kmap_num = kmap_end - kmap_offset + 1;
  351. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  352. switch (ret) {
  353. case 0:
  354. break;
  355. case -EBUSY:
  356. return -EAGAIN;
  357. default:
  358. return ret;
  359. }
  360. ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
  361. if (unlikely(ret != 0)) {
  362. ttm_bo_unreserve(bo);
  363. return ret;
  364. }
  365. virtual = ttm_kmap_obj_virtual(&map, &dummy);
  366. virtual += page_offset;
  367. if (write)
  368. ret = copy_from_user(virtual, wbuf, io_size);
  369. else
  370. ret = copy_to_user(rbuf, virtual, io_size);
  371. ttm_bo_kunmap(&map);
  372. ttm_bo_unreserve(bo);
  373. ttm_bo_unref(&bo);
  374. if (unlikely(ret != 0))
  375. return ret;
  376. *f_pos += io_size;
  377. return io_size;
  378. }