drm_vm.c 18 KB

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