drm_vm.c 18 KB

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