drm_vm.c 17 KB

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