drm_vm.c 17 KB

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