drm_vm.c 18 KB

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