drm_vm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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_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_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_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_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_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. /** AGP virtual memory operations */
  313. static const struct vm_operations_struct drm_vm_ops = {
  314. .fault = drm_vm_fault,
  315. .open = drm_vm_open,
  316. .close = drm_vm_close,
  317. };
  318. /** Shared virtual memory operations */
  319. static const struct vm_operations_struct drm_vm_shm_ops = {
  320. .fault = drm_vm_shm_fault,
  321. .open = drm_vm_open,
  322. .close = drm_vm_shm_close,
  323. };
  324. /** DMA virtual memory operations */
  325. static const struct vm_operations_struct drm_vm_dma_ops = {
  326. .fault = drm_vm_dma_fault,
  327. .open = drm_vm_open,
  328. .close = drm_vm_close,
  329. };
  330. /** Scatter-gather virtual memory operations */
  331. static const struct vm_operations_struct drm_vm_sg_ops = {
  332. .fault = drm_vm_sg_fault,
  333. .open = drm_vm_open,
  334. .close = drm_vm_close,
  335. };
  336. static void drm_vm_open_locked(struct drm_device *dev,
  337. struct vm_area_struct *vma)
  338. {
  339. struct drm_vma_entry *vma_entry;
  340. DRM_DEBUG("0x%08lx,0x%08lx\n",
  341. vma->vm_start, vma->vm_end - vma->vm_start);
  342. vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
  343. if (vma_entry) {
  344. vma_entry->vma = vma;
  345. vma_entry->pid = current->pid;
  346. list_add(&vma_entry->head, &dev->vmalist);
  347. }
  348. }
  349. static void drm_vm_open(struct vm_area_struct *vma)
  350. {
  351. struct drm_file *priv = vma->vm_file->private_data;
  352. struct drm_device *dev = priv->minor->dev;
  353. mutex_lock(&dev->struct_mutex);
  354. drm_vm_open_locked(dev, vma);
  355. mutex_unlock(&dev->struct_mutex);
  356. }
  357. static void drm_vm_close_locked(struct drm_device *dev,
  358. struct vm_area_struct *vma)
  359. {
  360. struct drm_vma_entry *pt, *temp;
  361. DRM_DEBUG("0x%08lx,0x%08lx\n",
  362. vma->vm_start, vma->vm_end - vma->vm_start);
  363. list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
  364. if (pt->vma == vma) {
  365. list_del(&pt->head);
  366. kfree(pt);
  367. break;
  368. }
  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. struct drm_file *priv = vma->vm_file->private_data;
  382. struct drm_device *dev = priv->minor->dev;
  383. mutex_lock(&dev->struct_mutex);
  384. drm_vm_close_locked(dev, vma);
  385. mutex_unlock(&dev->struct_mutex);
  386. }
  387. /**
  388. * mmap DMA memory.
  389. *
  390. * \param file_priv DRM file private.
  391. * \param vma virtual memory area.
  392. * \return zero on success or a negative number on failure.
  393. *
  394. * Sets the virtual memory area operations structure to vm_dma_ops, the file
  395. * pointer, and calls vm_open().
  396. */
  397. static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
  398. {
  399. struct drm_file *priv = filp->private_data;
  400. struct drm_device *dev;
  401. struct drm_device_dma *dma;
  402. unsigned long length = vma->vm_end - vma->vm_start;
  403. dev = priv->minor->dev;
  404. dma = dev->dma;
  405. DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
  406. vma->vm_start, vma->vm_end, vma->vm_pgoff);
  407. /* Length must match exact page count */
  408. if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
  409. return -EINVAL;
  410. }
  411. if (!capable(CAP_SYS_ADMIN) &&
  412. (dma->flags & _DRM_DMA_USE_PCI_RO)) {
  413. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  414. #if defined(__i386__) || defined(__x86_64__)
  415. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  416. #else
  417. /* Ye gads this is ugly. With more thought
  418. we could move this up higher and use
  419. `protection_map' instead. */
  420. vma->vm_page_prot =
  421. __pgprot(pte_val
  422. (pte_wrprotect
  423. (__pte(pgprot_val(vma->vm_page_prot)))));
  424. #endif
  425. }
  426. vma->vm_ops = &drm_vm_dma_ops;
  427. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  428. drm_vm_open_locked(dev, vma);
  429. return 0;
  430. }
  431. static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
  432. {
  433. #ifdef __alpha__
  434. return dev->hose->dense_mem_base;
  435. #else
  436. return 0;
  437. #endif
  438. }
  439. /**
  440. * mmap DMA memory.
  441. *
  442. * \param file_priv DRM file private.
  443. * \param vma virtual memory area.
  444. * \return zero on success or a negative number on failure.
  445. *
  446. * If the virtual memory area has no offset associated with it then it's a DMA
  447. * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
  448. * checks that the restricted flag is not set, sets the virtual memory operations
  449. * according to the mapping type and remaps the pages. Finally sets the file
  450. * pointer and calls vm_open().
  451. */
  452. static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
  453. {
  454. struct drm_file *priv = filp->private_data;
  455. struct drm_device *dev = priv->minor->dev;
  456. struct drm_local_map *map = NULL;
  457. resource_size_t offset = 0;
  458. struct drm_hash_item *hash;
  459. DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
  460. vma->vm_start, vma->vm_end, vma->vm_pgoff);
  461. if (!priv->authenticated)
  462. return -EACCES;
  463. /* We check for "dma". On Apple's UniNorth, it's valid to have
  464. * the AGP mapped at physical address 0
  465. * --BenH.
  466. */
  467. if (!vma->vm_pgoff
  468. #if IS_ENABLED(CONFIG_AGP)
  469. && (!dev->agp
  470. || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
  471. #endif
  472. )
  473. return drm_mmap_dma(filp, vma);
  474. if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
  475. DRM_ERROR("Could not find map\n");
  476. return -EINVAL;
  477. }
  478. map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
  479. if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
  480. return -EPERM;
  481. /* Check for valid size. */
  482. if (map->size < vma->vm_end - vma->vm_start)
  483. return -EINVAL;
  484. if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
  485. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  486. #if defined(__i386__) || defined(__x86_64__)
  487. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  488. #else
  489. /* Ye gads this is ugly. With more thought
  490. we could move this up higher and use
  491. `protection_map' instead. */
  492. vma->vm_page_prot =
  493. __pgprot(pte_val
  494. (pte_wrprotect
  495. (__pte(pgprot_val(vma->vm_page_prot)))));
  496. #endif
  497. }
  498. switch (map->type) {
  499. #if !defined(__arm__)
  500. case _DRM_AGP:
  501. if (dev->agp && dev->agp->cant_use_aperture) {
  502. /*
  503. * On some platforms we can't talk to bus dma address from the CPU, so for
  504. * memory of type DRM_AGP, we'll deal with sorting out the real physical
  505. * pages and mappings in fault()
  506. */
  507. #if defined(__powerpc__)
  508. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  509. #endif
  510. vma->vm_ops = &drm_vm_ops;
  511. break;
  512. }
  513. /* fall through to _DRM_FRAME_BUFFER... */
  514. #endif
  515. case _DRM_FRAME_BUFFER:
  516. case _DRM_REGISTERS:
  517. offset = drm_core_get_reg_ofs(dev);
  518. vma->vm_page_prot = drm_io_prot(map, vma);
  519. if (io_remap_pfn_range(vma, vma->vm_start,
  520. (map->offset + offset) >> PAGE_SHIFT,
  521. vma->vm_end - vma->vm_start,
  522. vma->vm_page_prot))
  523. return -EAGAIN;
  524. DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
  525. " offset = 0x%llx\n",
  526. map->type,
  527. vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
  528. vma->vm_ops = &drm_vm_ops;
  529. break;
  530. case _DRM_CONSISTENT:
  531. /* Consistent memory is really like shared memory. But
  532. * it's allocated in a different way, so avoid fault */
  533. if (remap_pfn_range(vma, vma->vm_start,
  534. page_to_pfn(virt_to_page(map->handle)),
  535. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  536. return -EAGAIN;
  537. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  538. /* fall through to _DRM_SHM */
  539. case _DRM_SHM:
  540. vma->vm_ops = &drm_vm_shm_ops;
  541. vma->vm_private_data = (void *)map;
  542. break;
  543. case _DRM_SCATTER_GATHER:
  544. vma->vm_ops = &drm_vm_sg_ops;
  545. vma->vm_private_data = (void *)map;
  546. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  547. break;
  548. default:
  549. return -EINVAL; /* This should never happen. */
  550. }
  551. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  552. drm_vm_open_locked(dev, vma);
  553. return 0;
  554. }
  555. int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)
  556. {
  557. struct drm_file *priv = filp->private_data;
  558. struct drm_device *dev = priv->minor->dev;
  559. int ret;
  560. if (drm_device_is_unplugged(dev))
  561. return -ENODEV;
  562. mutex_lock(&dev->struct_mutex);
  563. ret = drm_mmap_locked(filp, vma);
  564. mutex_unlock(&dev->struct_mutex);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL(drm_legacy_mmap);
  568. void drm_legacy_vma_flush(struct drm_device *dev)
  569. {
  570. struct drm_vma_entry *vma, *vma_temp;
  571. /* Clear vma list (only needed for legacy drivers) */
  572. list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
  573. list_del(&vma->head);
  574. kfree(vma);
  575. }
  576. }