drm_vm.c 19 KB

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