drm_vm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 |= _PAGE_NO_CACHE;
  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_area_struct *vma, struct vm_fault *vmf)
  91. {
  92. struct drm_file *priv = vma->vm_file->private_data;
  93. struct drm_device *dev = priv->minor->dev;
  94. struct drm_local_map *map = NULL;
  95. struct drm_map_list *r_list;
  96. struct drm_hash_item *hash;
  97. /*
  98. * Find the right map
  99. */
  100. if (!dev->agp)
  101. goto vm_fault_error;
  102. if (!dev->agp || !dev->agp->cant_use_aperture)
  103. goto vm_fault_error;
  104. if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
  105. goto vm_fault_error;
  106. r_list = drm_hash_entry(hash, struct drm_map_list, hash);
  107. map = r_list->map;
  108. if (map && map->type == _DRM_AGP) {
  109. /*
  110. * Using vm_pgoff as a selector forces us to use this unusual
  111. * addressing scheme.
  112. */
  113. resource_size_t offset = (unsigned long)vmf->virtual_address -
  114. 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_area_struct *vma, 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_area_struct *vma, struct vm_fault *vmf)
  169. {
  170. struct drm_local_map *map = vma->vm_private_data;
  171. unsigned long offset;
  172. unsigned long i;
  173. struct page *page;
  174. if (!map)
  175. return VM_FAULT_SIGBUS; /* Nothing allocated */
  176. offset = (unsigned long)vmf->virtual_address - vma->vm_start;
  177. i = (unsigned long)map->handle + offset;
  178. page = vmalloc_to_page((void *)i);
  179. if (!page)
  180. return VM_FAULT_SIGBUS;
  181. get_page(page);
  182. vmf->page = page;
  183. DRM_DEBUG("shm_fault 0x%lx\n", offset);
  184. return 0;
  185. }
  186. /**
  187. * \c close method for shared virtual memory.
  188. *
  189. * \param vma virtual memory area.
  190. *
  191. * Deletes map information if we are the last
  192. * person to close a mapping and it's not in the global maplist.
  193. */
  194. static void drm_vm_shm_close(struct vm_area_struct *vma)
  195. {
  196. struct drm_file *priv = vma->vm_file->private_data;
  197. struct drm_device *dev = priv->minor->dev;
  198. struct drm_vma_entry *pt, *temp;
  199. struct drm_local_map *map;
  200. struct drm_map_list *r_list;
  201. int found_maps = 0;
  202. DRM_DEBUG("0x%08lx,0x%08lx\n",
  203. vma->vm_start, vma->vm_end - vma->vm_start);
  204. map = vma->vm_private_data;
  205. mutex_lock(&dev->struct_mutex);
  206. list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
  207. if (pt->vma->vm_private_data == map)
  208. found_maps++;
  209. if (pt->vma == vma) {
  210. list_del(&pt->head);
  211. kfree(pt);
  212. }
  213. }
  214. /* We were the only map that was found */
  215. if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
  216. /* Check to see if we are in the maplist, if we are not, then
  217. * we delete this mappings information.
  218. */
  219. found_maps = 0;
  220. list_for_each_entry(r_list, &dev->maplist, head) {
  221. if (r_list->map == map)
  222. found_maps++;
  223. }
  224. if (!found_maps) {
  225. drm_dma_handle_t dmah;
  226. switch (map->type) {
  227. case _DRM_REGISTERS:
  228. case _DRM_FRAME_BUFFER:
  229. arch_phys_wc_del(map->mtrr);
  230. iounmap(map->handle);
  231. break;
  232. case _DRM_SHM:
  233. vfree(map->handle);
  234. break;
  235. case _DRM_AGP:
  236. case _DRM_SCATTER_GATHER:
  237. break;
  238. case _DRM_CONSISTENT:
  239. dmah.vaddr = map->handle;
  240. dmah.busaddr = map->offset;
  241. dmah.size = map->size;
  242. __drm_legacy_pci_free(dev, &dmah);
  243. break;
  244. }
  245. kfree(map);
  246. }
  247. }
  248. mutex_unlock(&dev->struct_mutex);
  249. }
  250. /**
  251. * \c fault method for DMA virtual memory.
  252. *
  253. * \param vma virtual memory area.
  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_area_struct *vma, struct vm_fault *vmf)
  260. {
  261. struct drm_file *priv = vma->vm_file->private_data;
  262. struct drm_device *dev = priv->minor->dev;
  263. struct drm_device_dma *dma = dev->dma;
  264. unsigned long offset;
  265. unsigned long page_nr;
  266. struct page *page;
  267. if (!dma)
  268. return VM_FAULT_SIGBUS; /* Error */
  269. if (!dma->pagelist)
  270. return VM_FAULT_SIGBUS; /* Nothing allocated */
  271. offset = (unsigned long)vmf->virtual_address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
  272. page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
  273. page = virt_to_page((void *)dma->pagelist[page_nr]);
  274. get_page(page);
  275. vmf->page = page;
  276. DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
  277. return 0;
  278. }
  279. /**
  280. * \c fault method for scatter-gather virtual memory.
  281. *
  282. * \param vma virtual memory area.
  283. * \param address access address.
  284. * \return pointer to the page structure.
  285. *
  286. * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
  287. */
  288. static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  289. {
  290. struct drm_local_map *map = vma->vm_private_data;
  291. struct drm_file *priv = vma->vm_file->private_data;
  292. struct drm_device *dev = priv->minor->dev;
  293. struct drm_sg_mem *entry = dev->sg;
  294. unsigned long offset;
  295. unsigned long map_offset;
  296. unsigned long page_offset;
  297. struct page *page;
  298. if (!entry)
  299. return VM_FAULT_SIGBUS; /* Error */
  300. if (!entry->pagelist)
  301. return VM_FAULT_SIGBUS; /* Nothing allocated */
  302. offset = (unsigned long)vmf->virtual_address - vma->vm_start;
  303. map_offset = map->offset - (unsigned long)dev->sg->virtual;
  304. page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
  305. page = entry->pagelist[page_offset];
  306. get_page(page);
  307. vmf->page = page;
  308. return 0;
  309. }
  310. static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  311. {
  312. return drm_do_vm_fault(vma, vmf);
  313. }
  314. static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  315. {
  316. return drm_do_vm_shm_fault(vma, vmf);
  317. }
  318. static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  319. {
  320. return drm_do_vm_dma_fault(vma, vmf);
  321. }
  322. static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  323. {
  324. return drm_do_vm_sg_fault(vma, vmf);
  325. }
  326. /** AGP virtual memory operations */
  327. static const struct vm_operations_struct drm_vm_ops = {
  328. .fault = drm_vm_fault,
  329. .open = drm_vm_open,
  330. .close = drm_vm_close,
  331. };
  332. /** Shared virtual memory operations */
  333. static const struct vm_operations_struct drm_vm_shm_ops = {
  334. .fault = drm_vm_shm_fault,
  335. .open = drm_vm_open,
  336. .close = drm_vm_shm_close,
  337. };
  338. /** DMA virtual memory operations */
  339. static const struct vm_operations_struct drm_vm_dma_ops = {
  340. .fault = drm_vm_dma_fault,
  341. .open = drm_vm_open,
  342. .close = drm_vm_close,
  343. };
  344. /** Scatter-gather virtual memory operations */
  345. static const struct vm_operations_struct drm_vm_sg_ops = {
  346. .fault = drm_vm_sg_fault,
  347. .open = drm_vm_open,
  348. .close = drm_vm_close,
  349. };
  350. /**
  351. * \c open method for shared virtual memory.
  352. *
  353. * \param vma virtual memory area.
  354. *
  355. * Create a new drm_vma_entry structure as the \p vma private data entry and
  356. * add it to drm_device::vmalist.
  357. */
  358. void drm_vm_open_locked(struct drm_device *dev,
  359. struct vm_area_struct *vma)
  360. {
  361. struct drm_vma_entry *vma_entry;
  362. DRM_DEBUG("0x%08lx,0x%08lx\n",
  363. vma->vm_start, vma->vm_end - vma->vm_start);
  364. vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
  365. if (vma_entry) {
  366. vma_entry->vma = vma;
  367. vma_entry->pid = current->pid;
  368. list_add(&vma_entry->head, &dev->vmalist);
  369. }
  370. }
  371. static void drm_vm_open(struct vm_area_struct *vma)
  372. {
  373. struct drm_file *priv = vma->vm_file->private_data;
  374. struct drm_device *dev = priv->minor->dev;
  375. mutex_lock(&dev->struct_mutex);
  376. drm_vm_open_locked(dev, vma);
  377. mutex_unlock(&dev->struct_mutex);
  378. }
  379. void drm_vm_close_locked(struct drm_device *dev,
  380. struct vm_area_struct *vma)
  381. {
  382. struct drm_vma_entry *pt, *temp;
  383. DRM_DEBUG("0x%08lx,0x%08lx\n",
  384. vma->vm_start, vma->vm_end - vma->vm_start);
  385. list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
  386. if (pt->vma == vma) {
  387. list_del(&pt->head);
  388. kfree(pt);
  389. break;
  390. }
  391. }
  392. }
  393. /**
  394. * \c close method for all virtual memory types.
  395. *
  396. * \param vma virtual memory area.
  397. *
  398. * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
  399. * free it.
  400. */
  401. static void drm_vm_close(struct vm_area_struct *vma)
  402. {
  403. struct drm_file *priv = vma->vm_file->private_data;
  404. struct drm_device *dev = priv->minor->dev;
  405. mutex_lock(&dev->struct_mutex);
  406. drm_vm_close_locked(dev, vma);
  407. mutex_unlock(&dev->struct_mutex);
  408. }
  409. /**
  410. * mmap DMA memory.
  411. *
  412. * \param file_priv DRM file private.
  413. * \param vma virtual memory area.
  414. * \return zero on success or a negative number on failure.
  415. *
  416. * Sets the virtual memory area operations structure to vm_dma_ops, the file
  417. * pointer, and calls vm_open().
  418. */
  419. static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
  420. {
  421. struct drm_file *priv = filp->private_data;
  422. struct drm_device *dev;
  423. struct drm_device_dma *dma;
  424. unsigned long length = vma->vm_end - vma->vm_start;
  425. dev = priv->minor->dev;
  426. dma = dev->dma;
  427. DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
  428. vma->vm_start, vma->vm_end, vma->vm_pgoff);
  429. /* Length must match exact page count */
  430. if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
  431. return -EINVAL;
  432. }
  433. if (!capable(CAP_SYS_ADMIN) &&
  434. (dma->flags & _DRM_DMA_USE_PCI_RO)) {
  435. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  436. #if defined(__i386__) || defined(__x86_64__)
  437. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  438. #else
  439. /* Ye gads this is ugly. With more thought
  440. we could move this up higher and use
  441. `protection_map' instead. */
  442. vma->vm_page_prot =
  443. __pgprot(pte_val
  444. (pte_wrprotect
  445. (__pte(pgprot_val(vma->vm_page_prot)))));
  446. #endif
  447. }
  448. vma->vm_ops = &drm_vm_dma_ops;
  449. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  450. drm_vm_open_locked(dev, vma);
  451. return 0;
  452. }
  453. static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
  454. {
  455. #ifdef __alpha__
  456. return dev->hose->dense_mem_base;
  457. #else
  458. return 0;
  459. #endif
  460. }
  461. /**
  462. * mmap DMA memory.
  463. *
  464. * \param file_priv DRM file private.
  465. * \param vma virtual memory area.
  466. * \return zero on success or a negative number on failure.
  467. *
  468. * If the virtual memory area has no offset associated with it then it's a DMA
  469. * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
  470. * checks that the restricted flag is not set, sets the virtual memory operations
  471. * according to the mapping type and remaps the pages. Finally sets the file
  472. * pointer and calls vm_open().
  473. */
  474. static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
  475. {
  476. struct drm_file *priv = filp->private_data;
  477. struct drm_device *dev = priv->minor->dev;
  478. struct drm_local_map *map = NULL;
  479. resource_size_t offset = 0;
  480. struct drm_hash_item *hash;
  481. DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
  482. vma->vm_start, vma->vm_end, vma->vm_pgoff);
  483. if (!priv->authenticated)
  484. return -EACCES;
  485. /* We check for "dma". On Apple's UniNorth, it's valid to have
  486. * the AGP mapped at physical address 0
  487. * --BenH.
  488. */
  489. if (!vma->vm_pgoff
  490. #if IS_ENABLED(CONFIG_AGP)
  491. && (!dev->agp
  492. || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
  493. #endif
  494. )
  495. return drm_mmap_dma(filp, vma);
  496. if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
  497. DRM_ERROR("Could not find map\n");
  498. return -EINVAL;
  499. }
  500. map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
  501. if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
  502. return -EPERM;
  503. /* Check for valid size. */
  504. if (map->size < vma->vm_end - vma->vm_start)
  505. return -EINVAL;
  506. if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
  507. vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
  508. #if defined(__i386__) || defined(__x86_64__)
  509. pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
  510. #else
  511. /* Ye gads this is ugly. With more thought
  512. we could move this up higher and use
  513. `protection_map' instead. */
  514. vma->vm_page_prot =
  515. __pgprot(pte_val
  516. (pte_wrprotect
  517. (__pte(pgprot_val(vma->vm_page_prot)))));
  518. #endif
  519. }
  520. switch (map->type) {
  521. #if !defined(__arm__)
  522. case _DRM_AGP:
  523. if (dev->agp && dev->agp->cant_use_aperture) {
  524. /*
  525. * On some platforms we can't talk to bus dma address from the CPU, so for
  526. * memory of type DRM_AGP, we'll deal with sorting out the real physical
  527. * pages and mappings in fault()
  528. */
  529. #if defined(__powerpc__)
  530. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  531. #endif
  532. vma->vm_ops = &drm_vm_ops;
  533. break;
  534. }
  535. /* fall through to _DRM_FRAME_BUFFER... */
  536. #endif
  537. case _DRM_FRAME_BUFFER:
  538. case _DRM_REGISTERS:
  539. offset = drm_core_get_reg_ofs(dev);
  540. vma->vm_page_prot = drm_io_prot(map, vma);
  541. if (io_remap_pfn_range(vma, vma->vm_start,
  542. (map->offset + offset) >> PAGE_SHIFT,
  543. vma->vm_end - vma->vm_start,
  544. vma->vm_page_prot))
  545. return -EAGAIN;
  546. DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
  547. " offset = 0x%llx\n",
  548. map->type,
  549. vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
  550. vma->vm_ops = &drm_vm_ops;
  551. break;
  552. case _DRM_CONSISTENT:
  553. /* Consistent memory is really like shared memory. But
  554. * it's allocated in a different way, so avoid fault */
  555. if (remap_pfn_range(vma, vma->vm_start,
  556. page_to_pfn(virt_to_page(map->handle)),
  557. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  558. return -EAGAIN;
  559. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  560. /* fall through to _DRM_SHM */
  561. case _DRM_SHM:
  562. vma->vm_ops = &drm_vm_shm_ops;
  563. vma->vm_private_data = (void *)map;
  564. break;
  565. case _DRM_SCATTER_GATHER:
  566. vma->vm_ops = &drm_vm_sg_ops;
  567. vma->vm_private_data = (void *)map;
  568. vma->vm_page_prot = drm_dma_prot(map->type, vma);
  569. break;
  570. default:
  571. return -EINVAL; /* This should never happen. */
  572. }
  573. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  574. drm_vm_open_locked(dev, vma);
  575. return 0;
  576. }
  577. int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)
  578. {
  579. struct drm_file *priv = filp->private_data;
  580. struct drm_device *dev = priv->minor->dev;
  581. int ret;
  582. if (drm_device_is_unplugged(dev))
  583. return -ENODEV;
  584. mutex_lock(&dev->struct_mutex);
  585. ret = drm_mmap_locked(filp, vma);
  586. mutex_unlock(&dev->struct_mutex);
  587. return ret;
  588. }
  589. EXPORT_SYMBOL(drm_legacy_mmap);
  590. void drm_legacy_vma_flush(struct drm_device *dev)
  591. {
  592. struct drm_vma_entry *vma, *vma_temp;
  593. /* Clear vma list (only needed for legacy drivers) */
  594. list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
  595. list_del(&vma->head);
  596. kfree(vma);
  597. }
  598. }
  599. int drm_vma_info(struct seq_file *m, void *data)
  600. {
  601. struct drm_info_node *node = (struct drm_info_node *) m->private;
  602. struct drm_device *dev = node->minor->dev;
  603. struct drm_vma_entry *pt;
  604. struct vm_area_struct *vma;
  605. unsigned long vma_count = 0;
  606. #if defined(__i386__)
  607. unsigned int pgprot;
  608. #endif
  609. mutex_lock(&dev->struct_mutex);
  610. list_for_each_entry(pt, &dev->vmalist, head)
  611. vma_count++;
  612. seq_printf(m, "vma use count: %lu, high_memory = %pK, 0x%pK\n",
  613. vma_count, high_memory,
  614. (void *)(unsigned long)virt_to_phys(high_memory));
  615. list_for_each_entry(pt, &dev->vmalist, head) {
  616. vma = pt->vma;
  617. if (!vma)
  618. continue;
  619. seq_printf(m,
  620. "\n%5d 0x%pK-0x%pK %c%c%c%c%c%c 0x%08lx000",
  621. pt->pid,
  622. (void *)vma->vm_start, (void *)vma->vm_end,
  623. vma->vm_flags & VM_READ ? 'r' : '-',
  624. vma->vm_flags & VM_WRITE ? 'w' : '-',
  625. vma->vm_flags & VM_EXEC ? 'x' : '-',
  626. vma->vm_flags & VM_MAYSHARE ? 's' : 'p',
  627. vma->vm_flags & VM_LOCKED ? 'l' : '-',
  628. vma->vm_flags & VM_IO ? 'i' : '-',
  629. vma->vm_pgoff);
  630. #if defined(__i386__)
  631. pgprot = pgprot_val(vma->vm_page_prot);
  632. seq_printf(m, " %c%c%c%c%c%c%c%c%c",
  633. pgprot & _PAGE_PRESENT ? 'p' : '-',
  634. pgprot & _PAGE_RW ? 'w' : 'r',
  635. pgprot & _PAGE_USER ? 'u' : 's',
  636. pgprot & _PAGE_PWT ? 't' : 'b',
  637. pgprot & _PAGE_PCD ? 'u' : 'c',
  638. pgprot & _PAGE_ACCESSED ? 'a' : '-',
  639. pgprot & _PAGE_DIRTY ? 'd' : '-',
  640. pgprot & _PAGE_PSE ? 'm' : 'k',
  641. pgprot & _PAGE_GLOBAL ? 'g' : 'l');
  642. #endif
  643. seq_printf(m, "\n");
  644. }
  645. mutex_unlock(&dev->struct_mutex);
  646. return 0;
  647. }