drm_vm.c 18 KB

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