drm_vm.c 18 KB

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