drm_vm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 "drmP.h"
  35. #if defined(__ia64__)
  36. #include <linux/efi.h>
  37. #endif
  38. static void drm_vm_open(struct vm_area_struct *vma);
  39. static void drm_vm_close(struct vm_area_struct *vma);
  40. static pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
  41. {
  42. pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
  43. #if defined(__i386__) || defined(__x86_64__)
  44. if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
  45. pgprot_val(tmp) |= _PAGE_PCD;
  46. pgprot_val(tmp) &= ~_PAGE_PWT;
  47. }
  48. #elif defined(__powerpc__)
  49. pgprot_val(tmp) |= _PAGE_NO_CACHE;
  50. if (map_type == _DRM_REGISTERS)
  51. pgprot_val(tmp) |= _PAGE_GUARDED;
  52. #elif defined(__ia64__)
  53. if (efi_range_is_wc(vma->vm_start, vma->vm_end -
  54. vma->vm_start))
  55. tmp = pgprot_writecombine(tmp);
  56. else
  57. tmp = pgprot_noncached(tmp);
  58. #elif defined(__sparc__)
  59. tmp = pgprot_noncached(tmp);
  60. #endif
  61. return tmp;
  62. }
  63. static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
  64. {
  65. pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
  66. #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
  67. tmp |= _PAGE_NO_CACHE;
  68. #endif
  69. return tmp;
  70. }
  71. /**
  72. * \c fault method for AGP virtual memory.
  73. *
  74. * \param vma virtual memory area.
  75. * \param address access address.
  76. * \return pointer to the page structure.
  77. *
  78. * Find the right map and if it's AGP memory find the real physical page to
  79. * map, get the page, increment the use count and return it.
  80. */
  81. #if __OS_HAS_AGP
  82. static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  83. {
  84. struct drm_file *priv = vma->vm_file->private_data;
  85. struct drm_device *dev = priv->minor->dev;
  86. struct drm_map *map = NULL;
  87. struct drm_map_list *r_list;
  88. struct drm_hash_item *hash;
  89. /*
  90. * Find the right map
  91. */
  92. if (!drm_core_has_AGP(dev))
  93. goto vm_fault_error;
  94. if (!dev->agp || !dev->agp->cant_use_aperture)
  95. goto vm_fault_error;
  96. if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
  97. goto vm_fault_error;
  98. r_list = drm_hash_entry(hash, struct drm_map_list, hash);
  99. map = r_list->map;
  100. if (map && map->type == _DRM_AGP) {
  101. /*
  102. * Using vm_pgoff as a selector forces us to use this unusual
  103. * addressing scheme.
  104. */
  105. unsigned long offset = (unsigned long)vmf->virtual_address -
  106. vma->vm_start;
  107. unsigned long baddr = map->offset + offset;
  108. struct drm_agp_mem *agpmem;
  109. struct page *page;
  110. #ifdef __alpha__
  111. /*
  112. * Adjust to a bus-relative address
  113. */
  114. baddr -= dev->hose->mem_space->start;
  115. #endif
  116. /*
  117. * It's AGP memory - find the real physical page to map
  118. */
  119. list_for_each_entry(agpmem, &dev->agp->memory, head) {
  120. if (agpmem->bound <= baddr &&
  121. agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
  122. break;
  123. }
  124. if (!agpmem)
  125. goto vm_fault_error;
  126. /*
  127. * Get the page, inc the use count, and return it
  128. */
  129. offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
  130. page = virt_to_page(__va(agpmem->memory->memory[offset]));
  131. get_page(page);
  132. vmf->page = page;
  133. DRM_DEBUG
  134. ("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
  135. baddr, __va(agpmem->memory->memory[offset]), offset,
  136. page_count(page));
  137. return 0;
  138. }
  139. vm_fault_error:
  140. return VM_FAULT_SIGBUS; /* Disallow mremap */
  141. }
  142. #else /* __OS_HAS_AGP */
  143. static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  144. {
  145. return VM_FAULT_SIGBUS;
  146. }
  147. #endif /* __OS_HAS_AGP */
  148. /**
  149. * \c nopage method for shared virtual memory.
  150. *
  151. * \param vma virtual memory area.
  152. * \param address access address.
  153. * \return pointer to the page structure.
  154. *
  155. * Get the mapping, find the real physical page to map, get the page, and
  156. * return it.
  157. */
  158. static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  159. {
  160. struct drm_map *map = (struct drm_map *) vma->vm_private_data;
  161. unsigned long offset;
  162. unsigned long i;
  163. struct page *page;
  164. if (!map)
  165. return VM_FAULT_SIGBUS; /* Nothing allocated */
  166. offset = (unsigned long)vmf->virtual_address - vma->vm_start;
  167. i = (unsigned long)map->handle + offset;
  168. page = vmalloc_to_page((void *)i);
  169. if (!page)
  170. return VM_FAULT_SIGBUS;
  171. get_page(page);
  172. vmf->page = page;
  173. DRM_DEBUG("shm_fault 0x%lx\n", offset);
  174. return 0;
  175. }
  176. /**
  177. * \c close method for shared virtual memory.
  178. *
  179. * \param vma virtual memory area.
  180. *
  181. * Deletes map information if we are the last
  182. * person to close a mapping and it's not in the global maplist.
  183. */
  184. static void drm_vm_shm_close(struct vm_area_struct *vma)
  185. {
  186. struct drm_file *priv = vma->vm_file->private_data;
  187. struct drm_device *dev = priv->minor->dev;
  188. struct drm_vma_entry *pt, *temp;
  189. struct drm_map *map;
  190. struct drm_map_list *r_list;
  191. int found_maps = 0;
  192. DRM_DEBUG("0x%08lx,0x%08lx\n",
  193. vma->vm_start, vma->vm_end - vma->vm_start);
  194. atomic_dec(&dev->vma_count);
  195. map = vma->vm_private_data;
  196. mutex_lock(&dev->struct_mutex);
  197. list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
  198. if (pt->vma->vm_private_data == map)
  199. found_maps++;
  200. if (pt->vma == vma) {
  201. list_del(&pt->head);
  202. drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
  203. }
  204. }
  205. /* We were the only map that was found */
  206. if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
  207. /* Check to see if we are in the maplist, if we are not, then
  208. * we delete this mappings information.
  209. */
  210. found_maps = 0;
  211. list_for_each_entry(r_list, &dev->maplist, head) {
  212. if (r_list->map == map)
  213. found_maps++;
  214. }
  215. if (!found_maps) {
  216. drm_dma_handle_t dmah;
  217. switch (map->type) {
  218. case _DRM_REGISTERS:
  219. case _DRM_FRAME_BUFFER:
  220. if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
  221. int retcode;
  222. retcode = mtrr_del(map->mtrr,
  223. map->offset,
  224. map->size);
  225. DRM_DEBUG("mtrr_del = %d\n", retcode);
  226. }
  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. }
  242. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  243. }
  244. }
  245. mutex_unlock(&dev->struct_mutex);
  246. }
  247. /**
  248. * \c fault method for DMA virtual memory.
  249. *
  250. * \param vma virtual memory area.
  251. * \param address access address.
  252. * \return pointer to the page structure.
  253. *
  254. * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
  255. */
  256. static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  257. {
  258. struct drm_file *priv = vma->vm_file->private_data;
  259. struct drm_device *dev = priv->minor->dev;
  260. struct drm_device_dma *dma = dev->dma;
  261. unsigned long offset;
  262. unsigned long page_nr;
  263. struct page *page;
  264. if (!dma)
  265. return VM_FAULT_SIGBUS; /* Error */
  266. if (!dma->pagelist)
  267. return VM_FAULT_SIGBUS; /* Nothing allocated */
  268. offset = (unsigned long)vmf->virtual_address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
  269. page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
  270. page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
  271. get_page(page);
  272. vmf->page = page;
  273. DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
  274. return 0;
  275. }
  276. /**
  277. * \c fault method for scatter-gather virtual memory.
  278. *
  279. * \param vma virtual memory area.
  280. * \param address access address.
  281. * \return pointer to the page structure.
  282. *
  283. * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
  284. */
  285. static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  286. {
  287. struct drm_map *map = (struct drm_map *) vma->vm_private_data;
  288. struct drm_file *priv = vma->vm_file->private_data;
  289. struct drm_device *dev = priv->minor->dev;
  290. struct drm_sg_mem *entry = dev->sg;
  291. unsigned long offset;
  292. unsigned long map_offset;
  293. unsigned long page_offset;
  294. struct page *page;
  295. if (!entry)
  296. return VM_FAULT_SIGBUS; /* Error */
  297. if (!entry->pagelist)
  298. return VM_FAULT_SIGBUS; /* Nothing allocated */
  299. offset = (unsigned long)vmf->virtual_address - vma->vm_start;
  300. map_offset = map->offset - (unsigned long)dev->sg->virtual;
  301. page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
  302. page = entry->pagelist[page_offset];
  303. get_page(page);
  304. vmf->page = page;
  305. return 0;
  306. }
  307. static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  308. {
  309. return drm_do_vm_fault(vma, vmf);
  310. }
  311. static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  312. {
  313. return drm_do_vm_shm_fault(vma, vmf);
  314. }
  315. static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  316. {
  317. return drm_do_vm_dma_fault(vma, vmf);
  318. }
  319. static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  320. {
  321. return drm_do_vm_sg_fault(vma, vmf);
  322. }
  323. /** AGP virtual memory operations */
  324. static struct vm_operations_struct drm_vm_ops = {
  325. .fault = drm_vm_fault,
  326. .open = drm_vm_open,
  327. .close = drm_vm_close,
  328. };
  329. /** Shared virtual memory operations */
  330. static struct vm_operations_struct drm_vm_shm_ops = {
  331. .fault = drm_vm_shm_fault,
  332. .open = drm_vm_open,
  333. .close = drm_vm_shm_close,
  334. };
  335. /** DMA virtual memory operations */
  336. static struct vm_operations_struct drm_vm_dma_ops = {
  337. .fault = drm_vm_dma_fault,
  338. .open = drm_vm_open,
  339. .close = drm_vm_close,
  340. };
  341. /** Scatter-gather virtual memory operations */
  342. static struct vm_operations_struct drm_vm_sg_ops = {
  343. .fault = drm_vm_sg_fault,
  344. .open = drm_vm_open,
  345. .close = drm_vm_close,
  346. };
  347. /**
  348. * \c open method for shared virtual memory.
  349. *
  350. * \param vma virtual memory area.
  351. *
  352. * Create a new drm_vma_entry structure as the \p vma private data entry and
  353. * add it to drm_device::vmalist.
  354. */
  355. static void drm_vm_open_locked(struct vm_area_struct *vma)
  356. {
  357. struct drm_file *priv = vma->vm_file->private_data;
  358. struct drm_device *dev = priv->minor->dev;
  359. struct drm_vma_entry *vma_entry;
  360. DRM_DEBUG("0x%08lx,0x%08lx\n",
  361. vma->vm_start, vma->vm_end - vma->vm_start);
  362. atomic_inc(&dev->vma_count);
  363. vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
  364. if (vma_entry) {
  365. vma_entry->vma = vma;
  366. vma_entry->pid = current->pid;
  367. list_add(&vma_entry->head, &dev->vmalist);
  368. }
  369. }
  370. static void drm_vm_open(struct vm_area_struct *vma)
  371. {
  372. struct drm_file *priv = vma->vm_file->private_data;
  373. struct drm_device *dev = priv->minor->dev;
  374. mutex_lock(&dev->struct_mutex);
  375. drm_vm_open_locked(vma);
  376. mutex_unlock(&dev->struct_mutex);
  377. }
  378. /**
  379. * \c close method for all virtual memory types.
  380. *
  381. * \param vma virtual memory area.
  382. *
  383. * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
  384. * free it.
  385. */
  386. static void drm_vm_close(struct vm_area_struct *vma)
  387. {
  388. struct drm_file *priv = vma->vm_file->private_data;
  389. struct drm_device *dev = priv->minor->dev;
  390. struct drm_vma_entry *pt, *temp;
  391. DRM_DEBUG("0x%08lx,0x%08lx\n",
  392. vma->vm_start, vma->vm_end - vma->vm_start);
  393. atomic_dec(&dev->vma_count);
  394. mutex_lock(&dev->struct_mutex);
  395. list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
  396. if (pt->vma == vma) {
  397. list_del(&pt->head);
  398. drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
  399. break;
  400. }
  401. }
  402. mutex_unlock(&dev->struct_mutex);
  403. }
  404. /**
  405. * mmap DMA memory.
  406. *
  407. * \param file_priv DRM file private.
  408. * \param vma virtual memory area.
  409. * \return zero on success or a negative number on failure.
  410. *
  411. * Sets the virtual memory area operations structure to vm_dma_ops, the file
  412. * pointer, and calls vm_open().
  413. */
  414. static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
  415. {
  416. struct drm_file *priv = filp->private_data;
  417. struct drm_device *dev;
  418. struct drm_device_dma *dma;
  419. unsigned long length = vma->vm_end - vma->vm_start;
  420. dev = priv->minor->dev;
  421. dma = dev->dma;
  422. DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
  423. vma->vm_start, vma->vm_end, vma->vm_pgoff);
  424. /* Length must match exact page count */
  425. if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
  426. return -EINVAL;
  427. }
  428. if (!capable(CAP_SYS_ADMIN) &&
  429. (dma->flags & _DRM_DMA_USE_PCI_RO)) {
  430. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  431. #if defined(__i386__) || defined(__x86_64__)
  432. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  433. #else
  434. /* Ye gads this is ugly. With more thought
  435. we could move this up higher and use
  436. `protection_map' instead. */
  437. vma->vm_page_prot =
  438. __pgprot(pte_val
  439. (pte_wrprotect
  440. (__pte(pgprot_val(vma->vm_page_prot)))));
  441. #endif
  442. }
  443. vma->vm_ops = &drm_vm_dma_ops;
  444. vma->vm_flags |= VM_RESERVED; /* Don't swap */
  445. vma->vm_flags |= VM_DONTEXPAND;
  446. vma->vm_file = filp; /* Needed for drm_vm_open() */
  447. drm_vm_open_locked(vma);
  448. return 0;
  449. }
  450. unsigned long drm_core_get_map_ofs(struct drm_map * map)
  451. {
  452. return map->offset;
  453. }
  454. EXPORT_SYMBOL(drm_core_get_map_ofs);
  455. unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
  456. {
  457. #ifdef __alpha__
  458. return dev->hose->dense_mem_base - dev->hose->mem_space->start;
  459. #else
  460. return 0;
  461. #endif
  462. }
  463. EXPORT_SYMBOL(drm_core_get_reg_ofs);
  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. static 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_map *map = NULL;
  482. unsigned long 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. case _DRM_AGP:
  525. if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
  526. /*
  527. * On some platforms we can't talk to bus dma address from the CPU, so for
  528. * memory of type DRM_AGP, we'll deal with sorting out the real physical
  529. * pages and mappings in fault()
  530. */
  531. #if defined(__powerpc__)
  532. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  533. #endif
  534. vma->vm_ops = &drm_vm_ops;
  535. break;
  536. }
  537. /* fall through to _DRM_FRAME_BUFFER... */
  538. case _DRM_FRAME_BUFFER:
  539. case _DRM_REGISTERS:
  540. offset = dev->driver->get_reg_ofs(dev);
  541. vma->vm_flags |= VM_IO; /* not in core dump */
  542. vma->vm_page_prot = drm_io_prot(map->type, vma);
  543. if (io_remap_pfn_range(vma, vma->vm_start,
  544. (map->offset + offset) >> PAGE_SHIFT,
  545. vma->vm_end - vma->vm_start,
  546. vma->vm_page_prot))
  547. return -EAGAIN;
  548. DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
  549. " offset = 0x%lx\n",
  550. map->type,
  551. vma->vm_start, vma->vm_end, map->offset + offset);
  552. vma->vm_ops = &drm_vm_ops;
  553. break;
  554. case _DRM_CONSISTENT:
  555. /* Consistent memory is really like shared memory. But
  556. * it's allocated in a different way, so avoid fault */
  557. if (remap_pfn_range(vma, vma->vm_start,
  558. page_to_pfn(virt_to_page(map->handle)),
  559. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  560. return -EAGAIN;
  561. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  562. /* fall through to _DRM_SHM */
  563. case _DRM_SHM:
  564. vma->vm_ops = &drm_vm_shm_ops;
  565. vma->vm_private_data = (void *)map;
  566. /* Don't let this area swap. Change when
  567. DRM_KERNEL advisory is supported. */
  568. vma->vm_flags |= VM_RESERVED;
  569. break;
  570. case _DRM_SCATTER_GATHER:
  571. vma->vm_ops = &drm_vm_sg_ops;
  572. vma->vm_private_data = (void *)map;
  573. vma->vm_flags |= VM_RESERVED;
  574. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  575. break;
  576. default:
  577. return -EINVAL; /* This should never happen. */
  578. }
  579. vma->vm_flags |= VM_RESERVED; /* Don't swap */
  580. vma->vm_flags |= VM_DONTEXPAND;
  581. vma->vm_file = filp; /* Needed for drm_vm_open() */
  582. drm_vm_open_locked(vma);
  583. return 0;
  584. }
  585. int drm_mmap(struct file *filp, struct vm_area_struct *vma)
  586. {
  587. struct drm_file *priv = filp->private_data;
  588. struct drm_device *dev = priv->minor->dev;
  589. int ret;
  590. mutex_lock(&dev->struct_mutex);
  591. ret = drm_mmap_locked(filp, vma);
  592. mutex_unlock(&dev->struct_mutex);
  593. return ret;
  594. }
  595. EXPORT_SYMBOL(drm_mmap);