/drivers/gpu/drm/drm_vm.c

https://bitbucket.org/cresqo/cm7-p500-kernel · C · 700 lines · 452 code · 82 blank · 166 comment · 64 complexity · 54d76f89e95d8d46580a9969d9ef784a MD5 · raw file

  1. /**
  2. * \file drm_vm.c
  3. * Memory mapping for DRM
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * Copyright (c) 2009, Code Aurora Forum.
  14. * All Rights Reserved.
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files (the "Software"),
  18. * to deal in the Software without restriction, including without limitation
  19. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. * and/or sell copies of the Software, and to permit persons to whom the
  21. * Software is furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice (including the next
  24. * paragraph) shall be included in all copies or substantial portions of the
  25. * Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  31. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  32. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33. * OTHER DEALINGS IN THE SOFTWARE.
  34. */
  35. #include "drmP.h"
  36. #if defined(__ia64__)
  37. #include <linux/efi.h>
  38. #include <linux/slab.h>
  39. #endif
  40. static void drm_vm_open(struct vm_area_struct *vma);
  41. static void drm_vm_close(struct vm_area_struct *vma);
  42. static pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
  43. {
  44. pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
  45. #if defined(__i386__) || defined(__x86_64__)
  46. if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
  47. pgprot_val(tmp) |= _PAGE_PCD;
  48. pgprot_val(tmp) &= ~_PAGE_PWT;
  49. }
  50. #elif defined(__powerpc__)
  51. pgprot_val(tmp) |= _PAGE_NO_CACHE;
  52. if (map_type == _DRM_REGISTERS)
  53. pgprot_val(tmp) |= _PAGE_GUARDED;
  54. #elif defined(__ia64__)
  55. if (efi_range_is_wc(vma->vm_start, vma->vm_end -
  56. vma->vm_start))
  57. tmp = pgprot_writecombine(tmp);
  58. else
  59. tmp = pgprot_noncached(tmp);
  60. #elif defined(__sparc__) || defined(__arm__)
  61. tmp = pgprot_noncached(tmp);
  62. #endif
  63. return tmp;
  64. }
  65. static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
  66. {
  67. pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
  68. #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
  69. tmp |= _PAGE_NO_CACHE;
  70. #endif
  71. return tmp;
  72. }
  73. /**
  74. * \c fault method for AGP virtual memory.
  75. *
  76. * \param vma virtual memory area.
  77. * \param address access address.
  78. * \return pointer to the page structure.
  79. *
  80. * Find the right map and if it's AGP memory find the real physical page to
  81. * map, get the page, increment the use count and return it.
  82. */
  83. #if __OS_HAS_AGP
  84. static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  85. {
  86. struct drm_file *priv = vma->vm_file->private_data;
  87. struct drm_device *dev = priv->minor->dev;
  88. struct drm_local_map *map = NULL;
  89. struct drm_map_list *r_list;
  90. struct drm_hash_item *hash;
  91. /*
  92. * Find the right map
  93. */
  94. if (!drm_core_has_AGP(dev))
  95. goto vm_fault_error;
  96. if (!dev->agp || !dev->agp->cant_use_aperture)
  97. goto vm_fault_error;
  98. if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
  99. goto vm_fault_error;
  100. r_list = drm_hash_entry(hash, struct drm_map_list, hash);
  101. map = r_list->map;
  102. if (map && map->type == _DRM_AGP) {
  103. /*
  104. * Using vm_pgoff as a selector forces us to use this unusual
  105. * addressing scheme.
  106. */
  107. resource_size_t offset = (unsigned long)vmf->virtual_address -
  108. vma->vm_start;
  109. resource_size_t baddr = map->offset + offset;
  110. struct drm_agp_mem *agpmem;
  111. struct page *page;
  112. #ifdef __alpha__
  113. /*
  114. * Adjust to a bus-relative address
  115. */
  116. baddr -= dev->hose->mem_space->start;
  117. #endif
  118. /*
  119. * It's AGP memory - find the real physical page to map
  120. */
  121. list_for_each_entry(agpmem, &dev->agp->memory, head) {
  122. if (agpmem->bound <= baddr &&
  123. agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
  124. break;
  125. }
  126. if (!agpmem)
  127. goto vm_fault_error;
  128. /*
  129. * Get the page, inc the use count, and return it
  130. */
  131. offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
  132. page = agpmem->memory->pages[offset];
  133. get_page(page);
  134. vmf->page = page;
  135. DRM_DEBUG
  136. ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
  137. (unsigned long long)baddr,
  138. agpmem->memory->pages[offset],
  139. (unsigned long long)offset,
  140. page_count(page));
  141. return 0;
  142. }
  143. vm_fault_error:
  144. return VM_FAULT_SIGBUS; /* Disallow mremap */
  145. }
  146. #else /* __OS_HAS_AGP */
  147. static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  148. {
  149. return VM_FAULT_SIGBUS;
  150. }
  151. #endif /* __OS_HAS_AGP */
  152. /**
  153. * \c nopage method for shared virtual memory.
  154. *
  155. * \param vma virtual memory area.
  156. * \param address access address.
  157. * \return pointer to the page structure.
  158. *
  159. * Get the mapping, find the real physical page to map, get the page, and
  160. * return it.
  161. */
  162. static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  163. {
  164. struct drm_local_map *map = vma->vm_private_data;
  165. unsigned long offset;
  166. unsigned long i;
  167. struct page *page;
  168. if (!map)
  169. return VM_FAULT_SIGBUS; /* Nothing allocated */
  170. offset = (unsigned long)vmf->virtual_address - vma->vm_start;
  171. i = (unsigned long)map->handle + offset;
  172. page = vmalloc_to_page((void *)i);
  173. if (!page)
  174. return VM_FAULT_SIGBUS;
  175. get_page(page);
  176. vmf->page = page;
  177. DRM_DEBUG("shm_fault 0x%lx\n", offset);
  178. return 0;
  179. }
  180. /**
  181. * \c close method for shared virtual memory.
  182. *
  183. * \param vma virtual memory area.
  184. *
  185. * Deletes map information if we are the last
  186. * person to close a mapping and it's not in the global maplist.
  187. */
  188. static void drm_vm_shm_close(struct vm_area_struct *vma)
  189. {
  190. struct drm_file *priv = vma->vm_file->private_data;
  191. struct drm_device *dev = priv->minor->dev;
  192. struct drm_vma_entry *pt, *temp;
  193. struct drm_local_map *map;
  194. struct drm_map_list *r_list;
  195. int found_maps = 0;
  196. DRM_DEBUG("0x%08lx,0x%08lx\n",
  197. vma->vm_start, vma->vm_end - vma->vm_start);
  198. atomic_dec(&dev->vma_count);
  199. map = vma->vm_private_data;
  200. mutex_lock(&dev->struct_mutex);
  201. list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
  202. if (pt->vma->vm_private_data == map)
  203. found_maps++;
  204. if (pt->vma == vma) {
  205. list_del(&pt->head);
  206. kfree(pt);
  207. }
  208. }
  209. /* We were the only map that was found */
  210. if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
  211. /* Check to see if we are in the maplist, if we are not, then
  212. * we delete this mappings information.
  213. */
  214. found_maps = 0;
  215. list_for_each_entry(r_list, &dev->maplist, head) {
  216. if (r_list->map == map)
  217. found_maps++;
  218. }
  219. if (!found_maps) {
  220. drm_dma_handle_t dmah;
  221. switch (map->type) {
  222. case _DRM_REGISTERS:
  223. case _DRM_FRAME_BUFFER:
  224. if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
  225. int retcode;
  226. retcode = mtrr_del(map->mtrr,
  227. map->offset,
  228. map->size);
  229. DRM_DEBUG("mtrr_del = %d\n", retcode);
  230. }
  231. iounmap(map->handle);
  232. break;
  233. case _DRM_SHM:
  234. vfree(map->handle);
  235. break;
  236. case _DRM_AGP:
  237. case _DRM_SCATTER_GATHER:
  238. break;
  239. case _DRM_CONSISTENT:
  240. dmah.vaddr = map->handle;
  241. dmah.busaddr = map->offset;
  242. dmah.size = map->size;
  243. __drm_pci_free(dev, &dmah);
  244. break;
  245. case _DRM_GEM:
  246. DRM_ERROR("tried to rmmap GEM object\n");
  247. break;
  248. }
  249. kfree(map);
  250. }
  251. }
  252. mutex_unlock(&dev->struct_mutex);
  253. }
  254. /**
  255. * \c fault method for DMA virtual memory.
  256. *
  257. * \param vma virtual memory area.
  258. * \param address access address.
  259. * \return pointer to the page structure.
  260. *
  261. * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
  262. */
  263. static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  264. {
  265. struct drm_file *priv = vma->vm_file->private_data;
  266. struct drm_device *dev = priv->minor->dev;
  267. struct drm_device_dma *dma = dev->dma;
  268. unsigned long offset;
  269. unsigned long page_nr;
  270. struct page *page;
  271. if (!dma)
  272. return VM_FAULT_SIGBUS; /* Error */
  273. if (!dma->pagelist)
  274. return VM_FAULT_SIGBUS; /* Nothing allocated */
  275. offset = (unsigned long)vmf->virtual_address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
  276. page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
  277. page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
  278. get_page(page);
  279. vmf->page = page;
  280. DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
  281. return 0;
  282. }
  283. /**
  284. * \c fault method for scatter-gather virtual memory.
  285. *
  286. * \param vma virtual memory area.
  287. * \param address access address.
  288. * \return pointer to the page structure.
  289. *
  290. * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
  291. */
  292. static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  293. {
  294. struct drm_local_map *map = vma->vm_private_data;
  295. struct drm_file *priv = vma->vm_file->private_data;
  296. struct drm_device *dev = priv->minor->dev;
  297. struct drm_sg_mem *entry = dev->sg;
  298. unsigned long offset;
  299. unsigned long map_offset;
  300. unsigned long page_offset;
  301. struct page *page;
  302. if (!entry)
  303. return VM_FAULT_SIGBUS; /* Error */
  304. if (!entry->pagelist)
  305. return VM_FAULT_SIGBUS; /* Nothing allocated */
  306. offset = (unsigned long)vmf->virtual_address - vma->vm_start;
  307. map_offset = map->offset - (unsigned long)dev->sg->virtual;
  308. page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
  309. page = entry->pagelist[page_offset];
  310. get_page(page);
  311. vmf->page = page;
  312. return 0;
  313. }
  314. static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  315. {
  316. return drm_do_vm_fault(vma, vmf);
  317. }
  318. static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  319. {
  320. return drm_do_vm_shm_fault(vma, vmf);
  321. }
  322. static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  323. {
  324. return drm_do_vm_dma_fault(vma, vmf);
  325. }
  326. static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  327. {
  328. return drm_do_vm_sg_fault(vma, vmf);
  329. }
  330. /** AGP virtual memory operations */
  331. static const struct vm_operations_struct drm_vm_ops = {
  332. .fault = drm_vm_fault,
  333. .open = drm_vm_open,
  334. .close = drm_vm_close,
  335. };
  336. /** Shared virtual memory operations */
  337. static const struct vm_operations_struct drm_vm_shm_ops = {
  338. .fault = drm_vm_shm_fault,
  339. .open = drm_vm_open,
  340. .close = drm_vm_shm_close,
  341. };
  342. /** DMA virtual memory operations */
  343. static const struct vm_operations_struct drm_vm_dma_ops = {
  344. .fault = drm_vm_dma_fault,
  345. .open = drm_vm_open,
  346. .close = drm_vm_close,
  347. };
  348. /** Scatter-gather virtual memory operations */
  349. static const struct vm_operations_struct drm_vm_sg_ops = {
  350. .fault = drm_vm_sg_fault,
  351. .open = drm_vm_open,
  352. .close = drm_vm_close,
  353. };
  354. /**
  355. * \c open method for shared virtual memory.
  356. *
  357. * \param vma virtual memory area.
  358. *
  359. * Create a new drm_vma_entry structure as the \p vma private data entry and
  360. * add it to drm_device::vmalist.
  361. */
  362. void drm_vm_open_locked(struct vm_area_struct *vma)
  363. {
  364. struct drm_file *priv = vma->vm_file->private_data;
  365. struct drm_device *dev = priv->minor->dev;
  366. struct drm_vma_entry *vma_entry;
  367. DRM_DEBUG("0x%08lx,0x%08lx\n",
  368. vma->vm_start, vma->vm_end - vma->vm_start);
  369. atomic_inc(&dev->vma_count);
  370. vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
  371. if (vma_entry) {
  372. vma_entry->vma = vma;
  373. vma_entry->pid = current->pid;
  374. list_add(&vma_entry->head, &dev->vmalist);
  375. }
  376. }
  377. static void drm_vm_open(struct vm_area_struct *vma)
  378. {
  379. struct drm_file *priv = vma->vm_file->private_data;
  380. struct drm_device *dev = priv->minor->dev;
  381. mutex_lock(&dev->struct_mutex);
  382. drm_vm_open_locked(vma);
  383. mutex_unlock(&dev->struct_mutex);
  384. }
  385. void drm_vm_close_locked(struct vm_area_struct *vma)
  386. {
  387. struct drm_file *priv = vma->vm_file->private_data;
  388. struct drm_device *dev = priv->minor->dev;
  389. struct drm_vma_entry *pt, *temp;
  390. DRM_DEBUG("0x%08lx,0x%08lx\n",
  391. vma->vm_start, vma->vm_end - vma->vm_start);
  392. atomic_dec(&dev->vma_count);
  393. list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
  394. if (pt->vma == vma) {
  395. list_del(&pt->head);
  396. kfree(pt);
  397. break;
  398. }
  399. }
  400. }
  401. /**
  402. * \c close method for all virtual memory types.
  403. *
  404. * \param vma virtual memory area.
  405. *
  406. * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
  407. * free it.
  408. */
  409. static void drm_vm_close(struct vm_area_struct *vma)
  410. {
  411. struct drm_file *priv = vma->vm_file->private_data;
  412. struct drm_device *dev = priv->minor->dev;
  413. mutex_lock(&dev->struct_mutex);
  414. drm_vm_close_locked(vma);
  415. mutex_unlock(&dev->struct_mutex);
  416. }
  417. /**
  418. * mmap DMA memory.
  419. *
  420. * \param file_priv DRM file private.
  421. * \param vma virtual memory area.
  422. * \return zero on success or a negative number on failure.
  423. *
  424. * Sets the virtual memory area operations structure to vm_dma_ops, the file
  425. * pointer, and calls vm_open().
  426. */
  427. static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
  428. {
  429. struct drm_file *priv = filp->private_data;
  430. struct drm_device *dev;
  431. struct drm_device_dma *dma;
  432. unsigned long length = vma->vm_end - vma->vm_start;
  433. dev = priv->minor->dev;
  434. dma = dev->dma;
  435. DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
  436. vma->vm_start, vma->vm_end, vma->vm_pgoff);
  437. /* Length must match exact page count */
  438. if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
  439. return -EINVAL;
  440. }
  441. if (!capable(CAP_SYS_ADMIN) &&
  442. (dma->flags & _DRM_DMA_USE_PCI_RO)) {
  443. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  444. #if defined(__i386__) || defined(__x86_64__)
  445. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  446. #else
  447. /* Ye gads this is ugly. With more thought
  448. we could move this up higher and use
  449. `protection_map' instead. */
  450. vma->vm_page_prot =
  451. __pgprot(pte_val
  452. (pte_wrprotect
  453. (__pte(pgprot_val(vma->vm_page_prot)))));
  454. #endif
  455. }
  456. vma->vm_ops = &drm_vm_dma_ops;
  457. vma->vm_flags |= VM_RESERVED; /* Don't swap */
  458. vma->vm_flags |= VM_DONTEXPAND;
  459. vma->vm_file = filp; /* Needed for drm_vm_open() */
  460. drm_vm_open_locked(vma);
  461. return 0;
  462. }
  463. resource_size_t drm_core_get_map_ofs(struct drm_local_map * map)
  464. {
  465. return map->offset;
  466. }
  467. EXPORT_SYMBOL(drm_core_get_map_ofs);
  468. resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
  469. {
  470. #ifdef __alpha__
  471. return dev->hose->dense_mem_base - dev->hose->mem_space->start;
  472. #else
  473. return 0;
  474. #endif
  475. }
  476. EXPORT_SYMBOL(drm_core_get_reg_ofs);
  477. /**
  478. * mmap DMA memory.
  479. *
  480. * \param file_priv DRM file private.
  481. * \param vma virtual memory area.
  482. * \return zero on success or a negative number on failure.
  483. *
  484. * If the virtual memory area has no offset associated with it then it's a DMA
  485. * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
  486. * checks that the restricted flag is not set, sets the virtual memory operations
  487. * according to the mapping type and remaps the pages. Finally sets the file
  488. * pointer and calls vm_open().
  489. */
  490. int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
  491. {
  492. struct drm_file *priv = filp->private_data;
  493. struct drm_device *dev = priv->minor->dev;
  494. struct drm_local_map *map = NULL;
  495. resource_size_t offset = 0;
  496. struct drm_hash_item *hash;
  497. DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
  498. vma->vm_start, vma->vm_end, vma->vm_pgoff);
  499. if (!priv->authenticated)
  500. return -EACCES;
  501. /* We check for "dma". On Apple's UniNorth, it's valid to have
  502. * the AGP mapped at physical address 0
  503. * --BenH.
  504. */
  505. if (!vma->vm_pgoff
  506. #if __OS_HAS_AGP
  507. && (!dev->agp
  508. || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
  509. #endif
  510. )
  511. return drm_mmap_dma(filp, vma);
  512. if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
  513. DRM_ERROR("Could not find map\n");
  514. return -EINVAL;
  515. }
  516. map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
  517. if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
  518. return -EPERM;
  519. /* Check for valid size. */
  520. if (map->size < vma->vm_end - vma->vm_start)
  521. return -EINVAL;
  522. if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
  523. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  524. #if defined(__i386__) || defined(__x86_64__)
  525. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  526. #else
  527. /* Ye gads this is ugly. With more thought
  528. we could move this up higher and use
  529. `protection_map' instead. */
  530. vma->vm_page_prot =
  531. __pgprot(pte_val
  532. (pte_wrprotect
  533. (__pte(pgprot_val(vma->vm_page_prot)))));
  534. #endif
  535. }
  536. switch (map->type) {
  537. #if !defined(__arm__)
  538. case _DRM_AGP:
  539. if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
  540. /*
  541. * On some platforms we can't talk to bus dma address from the CPU, so for
  542. * memory of type DRM_AGP, we'll deal with sorting out the real physical
  543. * pages and mappings in fault()
  544. */
  545. #if defined(__powerpc__)
  546. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  547. #endif
  548. vma->vm_ops = &drm_vm_ops;
  549. break;
  550. }
  551. /* fall through to _DRM_FRAME_BUFFER... */
  552. #endif
  553. case _DRM_FRAME_BUFFER:
  554. case _DRM_REGISTERS:
  555. offset = dev->driver->get_reg_ofs(dev);
  556. vma->vm_flags |= VM_IO; /* not in core dump */
  557. vma->vm_page_prot = drm_io_prot(map->type, vma);
  558. #if !defined(__arm__)
  559. if (io_remap_pfn_range(vma, vma->vm_start,
  560. (map->offset + offset) >> PAGE_SHIFT,
  561. vma->vm_end - vma->vm_start,
  562. vma->vm_page_prot))
  563. return -EAGAIN;
  564. #else
  565. if (remap_pfn_range(vma, vma->vm_start,
  566. (map->offset + offset) >> PAGE_SHIFT,
  567. vma->vm_end - vma->vm_start,
  568. vma->vm_page_prot))
  569. return -EAGAIN;
  570. #endif
  571. DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
  572. " offset = 0x%llx\n",
  573. map->type,
  574. vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
  575. vma->vm_ops = &drm_vm_ops;
  576. break;
  577. case _DRM_CONSISTENT:
  578. /* Consistent memory is really like shared memory. But
  579. * it's allocated in a different way, so avoid fault */
  580. if (remap_pfn_range(vma, vma->vm_start,
  581. page_to_pfn(virt_to_page(map->handle)),
  582. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  583. return -EAGAIN;
  584. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  585. /* fall through to _DRM_SHM */
  586. case _DRM_SHM:
  587. vma->vm_ops = &drm_vm_shm_ops;
  588. vma->vm_private_data = (void *)map;
  589. /* Don't let this area swap. Change when
  590. DRM_KERNEL advisory is supported. */
  591. vma->vm_flags |= VM_RESERVED;
  592. break;
  593. case _DRM_SCATTER_GATHER:
  594. vma->vm_ops = &drm_vm_sg_ops;
  595. vma->vm_private_data = (void *)map;
  596. vma->vm_flags |= VM_RESERVED;
  597. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  598. break;
  599. default:
  600. return -EINVAL; /* This should never happen. */
  601. }
  602. vma->vm_flags |= VM_RESERVED; /* Don't swap */
  603. vma->vm_flags |= VM_DONTEXPAND;
  604. vma->vm_file = filp; /* Needed for drm_vm_open() */
  605. drm_vm_open_locked(vma);
  606. return 0;
  607. }
  608. int drm_mmap(struct file *filp, struct vm_area_struct *vma)
  609. {
  610. struct drm_file *priv = filp->private_data;
  611. struct drm_device *dev = priv->minor->dev;
  612. int ret;
  613. mutex_lock(&dev->struct_mutex);
  614. ret = drm_mmap_locked(filp, vma);
  615. mutex_unlock(&dev->struct_mutex);
  616. return ret;
  617. }
  618. EXPORT_SYMBOL(drm_mmap);