drm_vm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. /**
  39. * \c nopage method for AGP virtual memory.
  40. *
  41. * \param vma virtual memory area.
  42. * \param address access address.
  43. * \return pointer to the page structure.
  44. *
  45. * Find the right map and if it's AGP memory find the real physical page to
  46. * map, get the page, increment the use count and return it.
  47. */
  48. #if __OS_HAS_AGP
  49. static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
  50. unsigned long address)
  51. {
  52. drm_file_t *priv = vma->vm_file->private_data;
  53. drm_device_t *dev = priv->head->dev;
  54. drm_map_t *map = NULL;
  55. drm_map_list_t *r_list;
  56. struct list_head *list;
  57. /*
  58. * Find the right map
  59. */
  60. if (!drm_core_has_AGP(dev))
  61. goto vm_nopage_error;
  62. if(!dev->agp || !dev->agp->cant_use_aperture) goto vm_nopage_error;
  63. list_for_each(list, &dev->maplist->head) {
  64. r_list = list_entry(list, drm_map_list_t, head);
  65. map = r_list->map;
  66. if (!map) continue;
  67. if (map->offset == VM_OFFSET(vma)) break;
  68. }
  69. if (map && map->type == _DRM_AGP) {
  70. unsigned long offset = address - vma->vm_start;
  71. unsigned long baddr = VM_OFFSET(vma) + offset;
  72. struct drm_agp_mem *agpmem;
  73. struct page *page;
  74. #ifdef __alpha__
  75. /*
  76. * Adjust to a bus-relative address
  77. */
  78. baddr -= dev->hose->mem_space->start;
  79. #endif
  80. /*
  81. * It's AGP memory - find the real physical page to map
  82. */
  83. for(agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next) {
  84. if (agpmem->bound <= baddr &&
  85. agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
  86. break;
  87. }
  88. if (!agpmem) goto vm_nopage_error;
  89. /*
  90. * Get the page, inc the use count, and return it
  91. */
  92. offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
  93. page = virt_to_page(__va(agpmem->memory->memory[offset]));
  94. get_page(page);
  95. DRM_DEBUG("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
  96. baddr, __va(agpmem->memory->memory[offset]), offset,
  97. page_count(page));
  98. return page;
  99. }
  100. vm_nopage_error:
  101. return NOPAGE_SIGBUS; /* Disallow mremap */
  102. }
  103. #else /* __OS_HAS_AGP */
  104. static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
  105. unsigned long address)
  106. {
  107. return NOPAGE_SIGBUS;
  108. }
  109. #endif /* __OS_HAS_AGP */
  110. /**
  111. * \c nopage method for shared virtual memory.
  112. *
  113. * \param vma virtual memory area.
  114. * \param address access address.
  115. * \return pointer to the page structure.
  116. *
  117. * Get the the mapping, find the real physical page to map, get the page, and
  118. * return it.
  119. */
  120. static __inline__ struct page *drm_do_vm_shm_nopage(struct vm_area_struct *vma,
  121. unsigned long address)
  122. {
  123. drm_map_t *map = (drm_map_t *)vma->vm_private_data;
  124. unsigned long offset;
  125. unsigned long i;
  126. struct page *page;
  127. if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
  128. if (!map) return NOPAGE_OOM; /* Nothing allocated */
  129. offset = address - vma->vm_start;
  130. i = (unsigned long)map->handle + offset;
  131. page = vmalloc_to_page((void *)i);
  132. if (!page)
  133. return NOPAGE_OOM;
  134. get_page(page);
  135. DRM_DEBUG("shm_nopage 0x%lx\n", address);
  136. return page;
  137. }
  138. /**
  139. * \c close method for shared virtual memory.
  140. *
  141. * \param vma virtual memory area.
  142. *
  143. * Deletes map information if we are the last
  144. * person to close a mapping and it's not in the global maplist.
  145. */
  146. void drm_vm_shm_close(struct vm_area_struct *vma)
  147. {
  148. drm_file_t *priv = vma->vm_file->private_data;
  149. drm_device_t *dev = priv->head->dev;
  150. drm_vma_entry_t *pt, *prev, *next;
  151. drm_map_t *map;
  152. drm_map_list_t *r_list;
  153. struct list_head *list;
  154. int found_maps = 0;
  155. DRM_DEBUG("0x%08lx,0x%08lx\n",
  156. vma->vm_start, vma->vm_end - vma->vm_start);
  157. atomic_dec(&dev->vma_count);
  158. map = vma->vm_private_data;
  159. down(&dev->struct_sem);
  160. for (pt = dev->vmalist, prev = NULL; pt; pt = next) {
  161. next = pt->next;
  162. if (pt->vma->vm_private_data == map) found_maps++;
  163. if (pt->vma == vma) {
  164. if (prev) {
  165. prev->next = pt->next;
  166. } else {
  167. dev->vmalist = pt->next;
  168. }
  169. drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
  170. } else {
  171. prev = pt;
  172. }
  173. }
  174. /* We were the only map that was found */
  175. if(found_maps == 1 &&
  176. map->flags & _DRM_REMOVABLE) {
  177. /* Check to see if we are in the maplist, if we are not, then
  178. * we delete this mappings information.
  179. */
  180. found_maps = 0;
  181. list = &dev->maplist->head;
  182. list_for_each(list, &dev->maplist->head) {
  183. r_list = list_entry(list, drm_map_list_t, head);
  184. if (r_list->map == map) found_maps++;
  185. }
  186. if(!found_maps) {
  187. switch (map->type) {
  188. case _DRM_REGISTERS:
  189. case _DRM_FRAME_BUFFER:
  190. if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
  191. int retcode;
  192. retcode = mtrr_del(map->mtrr,
  193. map->offset,
  194. map->size);
  195. DRM_DEBUG("mtrr_del = %d\n", retcode);
  196. }
  197. drm_ioremapfree(map->handle, map->size, dev);
  198. break;
  199. case _DRM_SHM:
  200. vfree(map->handle);
  201. break;
  202. case _DRM_AGP:
  203. case _DRM_SCATTER_GATHER:
  204. break;
  205. }
  206. drm_free(map, sizeof(*map), DRM_MEM_MAPS);
  207. }
  208. }
  209. up(&dev->struct_sem);
  210. }
  211. /**
  212. * \c nopage method for DMA virtual memory.
  213. *
  214. * \param vma virtual memory area.
  215. * \param address access address.
  216. * \return pointer to the page structure.
  217. *
  218. * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
  219. */
  220. static __inline__ struct page *drm_do_vm_dma_nopage(struct vm_area_struct *vma,
  221. unsigned long address)
  222. {
  223. drm_file_t *priv = vma->vm_file->private_data;
  224. drm_device_t *dev = priv->head->dev;
  225. drm_device_dma_t *dma = dev->dma;
  226. unsigned long offset;
  227. unsigned long page_nr;
  228. struct page *page;
  229. if (!dma) return NOPAGE_SIGBUS; /* Error */
  230. if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
  231. if (!dma->pagelist) return NOPAGE_OOM ; /* Nothing allocated */
  232. offset = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
  233. page_nr = offset >> PAGE_SHIFT;
  234. page = virt_to_page((dma->pagelist[page_nr] +
  235. (offset & (~PAGE_MASK))));
  236. get_page(page);
  237. DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address, page_nr);
  238. return page;
  239. }
  240. /**
  241. * \c nopage method for scatter-gather virtual memory.
  242. *
  243. * \param vma virtual memory area.
  244. * \param address access address.
  245. * \return pointer to the page structure.
  246. *
  247. * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
  248. */
  249. static __inline__ struct page *drm_do_vm_sg_nopage(struct vm_area_struct *vma,
  250. unsigned long address)
  251. {
  252. drm_map_t *map = (drm_map_t *)vma->vm_private_data;
  253. drm_file_t *priv = vma->vm_file->private_data;
  254. drm_device_t *dev = priv->head->dev;
  255. drm_sg_mem_t *entry = dev->sg;
  256. unsigned long offset;
  257. unsigned long map_offset;
  258. unsigned long page_offset;
  259. struct page *page;
  260. if (!entry) return NOPAGE_SIGBUS; /* Error */
  261. if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
  262. if (!entry->pagelist) return NOPAGE_OOM ; /* Nothing allocated */
  263. offset = address - vma->vm_start;
  264. map_offset = map->offset - dev->sg->handle;
  265. page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
  266. page = entry->pagelist[page_offset];
  267. get_page(page);
  268. return page;
  269. }
  270. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
  271. static struct page *drm_vm_nopage(struct vm_area_struct *vma,
  272. unsigned long address,
  273. int *type) {
  274. if (type) *type = VM_FAULT_MINOR;
  275. return drm_do_vm_nopage(vma, address);
  276. }
  277. static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
  278. unsigned long address,
  279. int *type) {
  280. if (type) *type = VM_FAULT_MINOR;
  281. return drm_do_vm_shm_nopage(vma, address);
  282. }
  283. static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
  284. unsigned long address,
  285. int *type) {
  286. if (type) *type = VM_FAULT_MINOR;
  287. return drm_do_vm_dma_nopage(vma, address);
  288. }
  289. static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
  290. unsigned long address,
  291. int *type) {
  292. if (type) *type = VM_FAULT_MINOR;
  293. return drm_do_vm_sg_nopage(vma, address);
  294. }
  295. #else /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
  296. static struct page *drm_vm_nopage(struct vm_area_struct *vma,
  297. unsigned long address,
  298. int unused) {
  299. return drm_do_vm_nopage(vma, address);
  300. }
  301. static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
  302. unsigned long address,
  303. int unused) {
  304. return drm_do_vm_shm_nopage(vma, address);
  305. }
  306. static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
  307. unsigned long address,
  308. int unused) {
  309. return drm_do_vm_dma_nopage(vma, address);
  310. }
  311. static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
  312. unsigned long address,
  313. int unused) {
  314. return drm_do_vm_sg_nopage(vma, address);
  315. }
  316. #endif
  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. 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. down(&dev->struct_sem);
  360. vma_entry->vma = vma;
  361. vma_entry->next = dev->vmalist;
  362. vma_entry->pid = current->pid;
  363. dev->vmalist = vma_entry;
  364. up(&dev->struct_sem);
  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. 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. down(&dev->struct_sem);
  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. up(&dev->struct_sem);
  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. 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, VM_OFFSET(vma));
  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. #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
  426. vma->vm_flags |= VM_LOCKED | VM_SHM; /* Don't swap */
  427. #else
  428. vma->vm_flags |= VM_RESERVED; /* Don't swap */
  429. #endif
  430. vma->vm_file = filp; /* Needed for drm_vm_open() */
  431. drm_vm_open(vma);
  432. return 0;
  433. }
  434. unsigned long drm_core_get_map_ofs(drm_map_t *map)
  435. {
  436. return map->offset;
  437. }
  438. EXPORT_SYMBOL(drm_core_get_map_ofs);
  439. unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
  440. {
  441. #ifdef __alpha__
  442. return dev->hose->dense_mem_base - dev->hose->mem_space->start;
  443. #else
  444. return 0;
  445. #endif
  446. }
  447. EXPORT_SYMBOL(drm_core_get_reg_ofs);
  448. /**
  449. * mmap DMA memory.
  450. *
  451. * \param filp file pointer.
  452. * \param vma virtual memory area.
  453. * \return zero on success or a negative number on failure.
  454. *
  455. * If the virtual memory area has no offset associated with it then it's a DMA
  456. * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
  457. * checks that the restricted flag is not set, sets the virtual memory operations
  458. * according to the mapping type and remaps the pages. Finally sets the file
  459. * pointer and calls vm_open().
  460. */
  461. int drm_mmap(struct file *filp, struct vm_area_struct *vma)
  462. {
  463. drm_file_t *priv = filp->private_data;
  464. drm_device_t *dev = priv->head->dev;
  465. drm_map_t *map = NULL;
  466. drm_map_list_t *r_list;
  467. unsigned long offset = 0;
  468. struct list_head *list;
  469. DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
  470. vma->vm_start, vma->vm_end, VM_OFFSET(vma));
  471. if ( !priv->authenticated ) return -EACCES;
  472. /* We check for "dma". On Apple's UniNorth, it's valid to have
  473. * the AGP mapped at physical address 0
  474. * --BenH.
  475. */
  476. if (!VM_OFFSET(vma)
  477. #if __OS_HAS_AGP
  478. && (!dev->agp || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
  479. #endif
  480. )
  481. return drm_mmap_dma(filp, vma);
  482. /* A sequential search of a linked list is
  483. fine here because: 1) there will only be
  484. about 5-10 entries in the list and, 2) a
  485. DRI client only has to do this mapping
  486. once, so it doesn't have to be optimized
  487. for performance, even if the list was a
  488. bit longer. */
  489. list_for_each(list, &dev->maplist->head) {
  490. unsigned long off;
  491. r_list = list_entry(list, drm_map_list_t, head);
  492. map = r_list->map;
  493. if (!map) continue;
  494. off = dev->driver->get_map_ofs(map);
  495. if (off == VM_OFFSET(vma)) break;
  496. }
  497. if (!map || ((map->flags&_DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
  498. return -EPERM;
  499. /* Check for valid size. */
  500. if (map->size != vma->vm_end - vma->vm_start) return -EINVAL;
  501. if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
  502. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  503. #if defined(__i386__) || defined(__x86_64__)
  504. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  505. #else
  506. /* Ye gads this is ugly. With more thought
  507. we could move this up higher and use
  508. `protection_map' instead. */
  509. vma->vm_page_prot = __pgprot(pte_val(pte_wrprotect(
  510. __pte(pgprot_val(vma->vm_page_prot)))));
  511. #endif
  512. }
  513. switch (map->type) {
  514. case _DRM_AGP:
  515. if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
  516. /*
  517. * On some platforms we can't talk to bus dma address from the CPU, so for
  518. * memory of type DRM_AGP, we'll deal with sorting out the real physical
  519. * pages and mappings in nopage()
  520. */
  521. #if defined(__powerpc__)
  522. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  523. #endif
  524. vma->vm_ops = &drm_vm_ops;
  525. break;
  526. }
  527. /* fall through to _DRM_FRAME_BUFFER... */
  528. case _DRM_FRAME_BUFFER:
  529. case _DRM_REGISTERS:
  530. if (VM_OFFSET(vma) >= __pa(high_memory)) {
  531. #if defined(__i386__) || defined(__x86_64__)
  532. if (boot_cpu_data.x86 > 3 && map->type != _DRM_AGP) {
  533. pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
  534. pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT;
  535. }
  536. #elif defined(__powerpc__)
  537. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  538. #endif
  539. vma->vm_flags |= VM_IO; /* not in core dump */
  540. }
  541. #if defined(__ia64__)
  542. if (efi_range_is_wc(vma->vm_start, vma->vm_end -
  543. vma->vm_start))
  544. vma->vm_page_prot =
  545. pgprot_writecombine(vma->vm_page_prot);
  546. else
  547. vma->vm_page_prot =
  548. pgprot_noncached(vma->vm_page_prot);
  549. #endif
  550. offset = dev->driver->get_reg_ofs(dev);
  551. #ifdef __sparc__
  552. if (io_remap_pfn_range(DRM_RPR_ARG(vma) vma->vm_start,
  553. (VM_OFFSET(vma) + offset) >> PAGE_SHIFT,
  554. vma->vm_end - vma->vm_start,
  555. vma->vm_page_prot))
  556. #else
  557. if (io_remap_pfn_range(vma, vma->vm_start,
  558. (VM_OFFSET(vma) + offset) >> PAGE_SHIFT,
  559. vma->vm_end - vma->vm_start,
  560. vma->vm_page_prot))
  561. #endif
  562. return -EAGAIN;
  563. DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
  564. " offset = 0x%lx\n",
  565. map->type,
  566. vma->vm_start, vma->vm_end, VM_OFFSET(vma) + offset);
  567. vma->vm_ops = &drm_vm_ops;
  568. break;
  569. case _DRM_SHM:
  570. vma->vm_ops = &drm_vm_shm_ops;
  571. vma->vm_private_data = (void *)map;
  572. /* Don't let this area swap. Change when
  573. DRM_KERNEL advisory is supported. */
  574. #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
  575. vma->vm_flags |= VM_LOCKED;
  576. #else
  577. vma->vm_flags |= VM_RESERVED;
  578. #endif
  579. break;
  580. case _DRM_SCATTER_GATHER:
  581. vma->vm_ops = &drm_vm_sg_ops;
  582. vma->vm_private_data = (void *)map;
  583. #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
  584. vma->vm_flags |= VM_LOCKED;
  585. #else
  586. vma->vm_flags |= VM_RESERVED;
  587. #endif
  588. break;
  589. default:
  590. return -EINVAL; /* This should never happen. */
  591. }
  592. #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
  593. vma->vm_flags |= VM_LOCKED | VM_SHM; /* Don't swap */
  594. #else
  595. vma->vm_flags |= VM_RESERVED; /* Don't swap */
  596. #endif
  597. vma->vm_file = filp; /* Needed for drm_vm_open() */
  598. drm_vm_open(vma);
  599. return 0;
  600. }
  601. EXPORT_SYMBOL(drm_mmap);