drm_vm.c 17 KB

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