drm_vm.c 19 KB

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