drm_bufs.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /*
  2. * Legacy: Generic DRM Buffer Management
  3. *
  4. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  5. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6. * All Rights Reserved.
  7. *
  8. * Author: Rickard E. (Rik) Faith <faith@valinux.com>
  9. * Author: Gareth Hughes <gareth@valinux.com>
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice (including the next
  19. * paragraph) shall be included in all copies or substantial portions of the
  20. * Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  26. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  27. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  28. * OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. #include <linux/vmalloc.h>
  31. #include <linux/slab.h>
  32. #include <linux/log2.h>
  33. #include <linux/export.h>
  34. #include <asm/shmparam.h>
  35. #include <drm/drmP.h>
  36. #include "drm_legacy.h"
  37. static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
  38. struct drm_local_map *map)
  39. {
  40. struct drm_map_list *entry;
  41. list_for_each_entry(entry, &dev->maplist, head) {
  42. /*
  43. * Because the kernel-userspace ABI is fixed at a 32-bit offset
  44. * while PCI resources may live above that, we only compare the
  45. * lower 32 bits of the map offset for maps of type
  46. * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
  47. * It is assumed that if a driver have more than one resource
  48. * of each type, the lower 32 bits are different.
  49. */
  50. if (!entry->map ||
  51. map->type != entry->map->type ||
  52. entry->master != dev->master)
  53. continue;
  54. switch (map->type) {
  55. case _DRM_SHM:
  56. if (map->flags != _DRM_CONTAINS_LOCK)
  57. break;
  58. return entry;
  59. case _DRM_REGISTERS:
  60. case _DRM_FRAME_BUFFER:
  61. if ((entry->map->offset & 0xffffffff) ==
  62. (map->offset & 0xffffffff))
  63. return entry;
  64. default: /* Make gcc happy */
  65. ;
  66. }
  67. if (entry->map->offset == map->offset)
  68. return entry;
  69. }
  70. return NULL;
  71. }
  72. static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
  73. unsigned long user_token, int hashed_handle, int shm)
  74. {
  75. int use_hashed_handle, shift;
  76. unsigned long add;
  77. #if (BITS_PER_LONG == 64)
  78. use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
  79. #elif (BITS_PER_LONG == 32)
  80. use_hashed_handle = hashed_handle;
  81. #else
  82. #error Unsupported long size. Neither 64 nor 32 bits.
  83. #endif
  84. if (!use_hashed_handle) {
  85. int ret;
  86. hash->key = user_token >> PAGE_SHIFT;
  87. ret = drm_ht_insert_item(&dev->map_hash, hash);
  88. if (ret != -EINVAL)
  89. return ret;
  90. }
  91. shift = 0;
  92. add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
  93. if (shm && (SHMLBA > PAGE_SIZE)) {
  94. int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
  95. /* For shared memory, we have to preserve the SHMLBA
  96. * bits of the eventual vma->vm_pgoff value during
  97. * mmap(). Otherwise we run into cache aliasing problems
  98. * on some platforms. On these platforms, the pgoff of
  99. * a mmap() request is used to pick a suitable virtual
  100. * address for the mmap() region such that it will not
  101. * cause cache aliasing problems.
  102. *
  103. * Therefore, make sure the SHMLBA relevant bits of the
  104. * hash value we use are equal to those in the original
  105. * kernel virtual address.
  106. */
  107. shift = bits;
  108. add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
  109. }
  110. return drm_ht_just_insert_please(&dev->map_hash, hash,
  111. user_token, 32 - PAGE_SHIFT - 3,
  112. shift, add);
  113. }
  114. /**
  115. * Core function to create a range of memory available for mapping by a
  116. * non-root process.
  117. *
  118. * Adjusts the memory offset to its absolute value according to the mapping
  119. * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
  120. * applicable and if supported by the kernel.
  121. */
  122. static int drm_addmap_core(struct drm_device *dev, resource_size_t offset,
  123. unsigned int size, enum drm_map_type type,
  124. enum drm_map_flags flags,
  125. struct drm_map_list **maplist)
  126. {
  127. struct drm_local_map *map;
  128. struct drm_map_list *list;
  129. drm_dma_handle_t *dmah;
  130. unsigned long user_token;
  131. int ret;
  132. map = kmalloc(sizeof(*map), GFP_KERNEL);
  133. if (!map)
  134. return -ENOMEM;
  135. map->offset = offset;
  136. map->size = size;
  137. map->flags = flags;
  138. map->type = type;
  139. /* Only allow shared memory to be removable since we only keep enough
  140. * book keeping information about shared memory to allow for removal
  141. * when processes fork.
  142. */
  143. if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
  144. kfree(map);
  145. return -EINVAL;
  146. }
  147. DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
  148. (unsigned long long)map->offset, map->size, map->type);
  149. /* page-align _DRM_SHM maps. They are allocated here so there is no security
  150. * hole created by that and it works around various broken drivers that use
  151. * a non-aligned quantity to map the SAREA. --BenH
  152. */
  153. if (map->type == _DRM_SHM)
  154. map->size = PAGE_ALIGN(map->size);
  155. if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
  156. kfree(map);
  157. return -EINVAL;
  158. }
  159. map->mtrr = -1;
  160. map->handle = NULL;
  161. switch (map->type) {
  162. case _DRM_REGISTERS:
  163. case _DRM_FRAME_BUFFER:
  164. #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
  165. if (map->offset + (map->size-1) < map->offset ||
  166. map->offset < virt_to_phys(high_memory)) {
  167. kfree(map);
  168. return -EINVAL;
  169. }
  170. #endif
  171. /* Some drivers preinitialize some maps, without the X Server
  172. * needing to be aware of it. Therefore, we just return success
  173. * when the server tries to create a duplicate map.
  174. */
  175. list = drm_find_matching_map(dev, map);
  176. if (list != NULL) {
  177. if (list->map->size != map->size) {
  178. DRM_DEBUG("Matching maps of type %d with "
  179. "mismatched sizes, (%ld vs %ld)\n",
  180. map->type, map->size,
  181. list->map->size);
  182. list->map->size = map->size;
  183. }
  184. kfree(map);
  185. *maplist = list;
  186. return 0;
  187. }
  188. if (map->type == _DRM_FRAME_BUFFER ||
  189. (map->flags & _DRM_WRITE_COMBINING)) {
  190. map->mtrr =
  191. arch_phys_wc_add(map->offset, map->size);
  192. }
  193. if (map->type == _DRM_REGISTERS) {
  194. if (map->flags & _DRM_WRITE_COMBINING)
  195. map->handle = ioremap_wc(map->offset,
  196. map->size);
  197. else
  198. map->handle = ioremap(map->offset, map->size);
  199. if (!map->handle) {
  200. kfree(map);
  201. return -ENOMEM;
  202. }
  203. }
  204. break;
  205. case _DRM_SHM:
  206. list = drm_find_matching_map(dev, map);
  207. if (list != NULL) {
  208. if (list->map->size != map->size) {
  209. DRM_DEBUG("Matching maps of type %d with "
  210. "mismatched sizes, (%ld vs %ld)\n",
  211. map->type, map->size, list->map->size);
  212. list->map->size = map->size;
  213. }
  214. kfree(map);
  215. *maplist = list;
  216. return 0;
  217. }
  218. map->handle = vmalloc_user(map->size);
  219. DRM_DEBUG("%lu %d %p\n",
  220. map->size, order_base_2(map->size), map->handle);
  221. if (!map->handle) {
  222. kfree(map);
  223. return -ENOMEM;
  224. }
  225. map->offset = (unsigned long)map->handle;
  226. if (map->flags & _DRM_CONTAINS_LOCK) {
  227. /* Prevent a 2nd X Server from creating a 2nd lock */
  228. if (dev->master->lock.hw_lock != NULL) {
  229. vfree(map->handle);
  230. kfree(map);
  231. return -EBUSY;
  232. }
  233. dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
  234. }
  235. break;
  236. case _DRM_AGP: {
  237. struct drm_agp_mem *entry;
  238. int valid = 0;
  239. if (!dev->agp) {
  240. kfree(map);
  241. return -EINVAL;
  242. }
  243. #ifdef __alpha__
  244. map->offset += dev->hose->mem_space->start;
  245. #endif
  246. /* In some cases (i810 driver), user space may have already
  247. * added the AGP base itself, because dev->agp->base previously
  248. * only got set during AGP enable. So, only add the base
  249. * address if the map's offset isn't already within the
  250. * aperture.
  251. */
  252. if (map->offset < dev->agp->base ||
  253. map->offset > dev->agp->base +
  254. dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
  255. map->offset += dev->agp->base;
  256. }
  257. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  258. /* This assumes the DRM is in total control of AGP space.
  259. * It's not always the case as AGP can be in the control
  260. * of user space (i.e. i810 driver). So this loop will get
  261. * skipped and we double check that dev->agp->memory is
  262. * actually set as well as being invalid before EPERM'ing
  263. */
  264. list_for_each_entry(entry, &dev->agp->memory, head) {
  265. if ((map->offset >= entry->bound) &&
  266. (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
  267. valid = 1;
  268. break;
  269. }
  270. }
  271. if (!list_empty(&dev->agp->memory) && !valid) {
  272. kfree(map);
  273. return -EPERM;
  274. }
  275. DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
  276. (unsigned long long)map->offset, map->size);
  277. break;
  278. }
  279. case _DRM_SCATTER_GATHER:
  280. if (!dev->sg) {
  281. kfree(map);
  282. return -EINVAL;
  283. }
  284. map->offset += (unsigned long)dev->sg->virtual;
  285. break;
  286. case _DRM_CONSISTENT:
  287. /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
  288. * As we're limiting the address to 2^32-1 (or less),
  289. * casting it down to 32 bits is no problem, but we
  290. * need to point to a 64bit variable first. */
  291. dmah = drm_pci_alloc(dev, map->size, map->size);
  292. if (!dmah) {
  293. kfree(map);
  294. return -ENOMEM;
  295. }
  296. map->handle = dmah->vaddr;
  297. map->offset = (unsigned long)dmah->busaddr;
  298. kfree(dmah);
  299. break;
  300. default:
  301. kfree(map);
  302. return -EINVAL;
  303. }
  304. list = kzalloc(sizeof(*list), GFP_KERNEL);
  305. if (!list) {
  306. if (map->type == _DRM_REGISTERS)
  307. iounmap(map->handle);
  308. kfree(map);
  309. return -EINVAL;
  310. }
  311. list->map = map;
  312. mutex_lock(&dev->struct_mutex);
  313. list_add(&list->head, &dev->maplist);
  314. /* Assign a 32-bit handle */
  315. /* We do it here so that dev->struct_mutex protects the increment */
  316. user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
  317. map->offset;
  318. ret = drm_map_handle(dev, &list->hash, user_token, 0,
  319. (map->type == _DRM_SHM));
  320. if (ret) {
  321. if (map->type == _DRM_REGISTERS)
  322. iounmap(map->handle);
  323. kfree(map);
  324. kfree(list);
  325. mutex_unlock(&dev->struct_mutex);
  326. return ret;
  327. }
  328. list->user_token = list->hash.key << PAGE_SHIFT;
  329. mutex_unlock(&dev->struct_mutex);
  330. if (!(map->flags & _DRM_DRIVER))
  331. list->master = dev->master;
  332. *maplist = list;
  333. return 0;
  334. }
  335. int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset,
  336. unsigned int size, enum drm_map_type type,
  337. enum drm_map_flags flags, struct drm_local_map **map_ptr)
  338. {
  339. struct drm_map_list *list;
  340. int rc;
  341. rc = drm_addmap_core(dev, offset, size, type, flags, &list);
  342. if (!rc)
  343. *map_ptr = list->map;
  344. return rc;
  345. }
  346. EXPORT_SYMBOL(drm_legacy_addmap);
  347. /**
  348. * Ioctl to specify a range of memory that is available for mapping by a
  349. * non-root process.
  350. *
  351. * \param inode device inode.
  352. * \param file_priv DRM file private.
  353. * \param cmd command.
  354. * \param arg pointer to a drm_map structure.
  355. * \return zero on success or a negative value on error.
  356. *
  357. */
  358. int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
  359. struct drm_file *file_priv)
  360. {
  361. struct drm_map *map = data;
  362. struct drm_map_list *maplist;
  363. int err;
  364. if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
  365. return -EPERM;
  366. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  367. !drm_core_check_feature(dev, DRIVER_LEGACY))
  368. return -EINVAL;
  369. err = drm_addmap_core(dev, map->offset, map->size, map->type,
  370. map->flags, &maplist);
  371. if (err)
  372. return err;
  373. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  374. map->handle = (void *)(unsigned long)maplist->user_token;
  375. /*
  376. * It appears that there are no users of this value whatsoever --
  377. * drmAddMap just discards it. Let's not encourage its use.
  378. * (Keeping drm_addmap_core's returned mtrr value would be wrong --
  379. * it's not a real mtrr index anymore.)
  380. */
  381. map->mtrr = -1;
  382. return 0;
  383. }
  384. /*
  385. * Get a mapping information.
  386. *
  387. * \param inode device inode.
  388. * \param file_priv DRM file private.
  389. * \param cmd command.
  390. * \param arg user argument, pointing to a drm_map structure.
  391. *
  392. * \return zero on success or a negative number on failure.
  393. *
  394. * Searches for the mapping with the specified offset and copies its information
  395. * into userspace
  396. */
  397. int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
  398. struct drm_file *file_priv)
  399. {
  400. struct drm_map *map = data;
  401. struct drm_map_list *r_list = NULL;
  402. struct list_head *list;
  403. int idx;
  404. int i;
  405. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  406. !drm_core_check_feature(dev, DRIVER_LEGACY))
  407. return -EINVAL;
  408. idx = map->offset;
  409. if (idx < 0)
  410. return -EINVAL;
  411. i = 0;
  412. mutex_lock(&dev->struct_mutex);
  413. list_for_each(list, &dev->maplist) {
  414. if (i == idx) {
  415. r_list = list_entry(list, struct drm_map_list, head);
  416. break;
  417. }
  418. i++;
  419. }
  420. if (!r_list || !r_list->map) {
  421. mutex_unlock(&dev->struct_mutex);
  422. return -EINVAL;
  423. }
  424. map->offset = r_list->map->offset;
  425. map->size = r_list->map->size;
  426. map->type = r_list->map->type;
  427. map->flags = r_list->map->flags;
  428. map->handle = (void *)(unsigned long) r_list->user_token;
  429. map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
  430. mutex_unlock(&dev->struct_mutex);
  431. return 0;
  432. }
  433. /**
  434. * Remove a map private from list and deallocate resources if the mapping
  435. * isn't in use.
  436. *
  437. * Searches the map on drm_device::maplist, removes it from the list, see if
  438. * its being used, and free any associate resource (such as MTRR's) if it's not
  439. * being on use.
  440. *
  441. * \sa drm_legacy_addmap
  442. */
  443. int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  444. {
  445. struct drm_map_list *r_list = NULL, *list_t;
  446. drm_dma_handle_t dmah;
  447. int found = 0;
  448. struct drm_master *master;
  449. /* Find the list entry for the map and remove it */
  450. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  451. if (r_list->map == map) {
  452. master = r_list->master;
  453. list_del(&r_list->head);
  454. drm_ht_remove_key(&dev->map_hash,
  455. r_list->user_token >> PAGE_SHIFT);
  456. kfree(r_list);
  457. found = 1;
  458. break;
  459. }
  460. }
  461. if (!found)
  462. return -EINVAL;
  463. switch (map->type) {
  464. case _DRM_REGISTERS:
  465. iounmap(map->handle);
  466. /* FALLTHROUGH */
  467. case _DRM_FRAME_BUFFER:
  468. arch_phys_wc_del(map->mtrr);
  469. break;
  470. case _DRM_SHM:
  471. vfree(map->handle);
  472. if (master) {
  473. if (dev->sigdata.lock == master->lock.hw_lock)
  474. dev->sigdata.lock = NULL;
  475. master->lock.hw_lock = NULL; /* SHM removed */
  476. master->lock.file_priv = NULL;
  477. wake_up_interruptible_all(&master->lock.lock_queue);
  478. }
  479. break;
  480. case _DRM_AGP:
  481. case _DRM_SCATTER_GATHER:
  482. break;
  483. case _DRM_CONSISTENT:
  484. dmah.vaddr = map->handle;
  485. dmah.busaddr = map->offset;
  486. dmah.size = map->size;
  487. __drm_legacy_pci_free(dev, &dmah);
  488. break;
  489. }
  490. kfree(map);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL(drm_legacy_rmmap_locked);
  494. void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
  495. {
  496. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  497. !drm_core_check_feature(dev, DRIVER_LEGACY))
  498. return;
  499. mutex_lock(&dev->struct_mutex);
  500. drm_legacy_rmmap_locked(dev, map);
  501. mutex_unlock(&dev->struct_mutex);
  502. }
  503. EXPORT_SYMBOL(drm_legacy_rmmap);
  504. void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
  505. {
  506. struct drm_map_list *r_list, *list_temp;
  507. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  508. return;
  509. mutex_lock(&dev->struct_mutex);
  510. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  511. if (r_list->master == master) {
  512. drm_legacy_rmmap_locked(dev, r_list->map);
  513. r_list = NULL;
  514. }
  515. }
  516. mutex_unlock(&dev->struct_mutex);
  517. }
  518. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  519. * the last close of the device, and this is necessary for cleanup when things
  520. * exit uncleanly. Therefore, having userland manually remove mappings seems
  521. * like a pointless exercise since they're going away anyway.
  522. *
  523. * One use case might be after addmap is allowed for normal users for SHM and
  524. * gets used by drivers that the server doesn't need to care about. This seems
  525. * unlikely.
  526. *
  527. * \param inode device inode.
  528. * \param file_priv DRM file private.
  529. * \param cmd command.
  530. * \param arg pointer to a struct drm_map structure.
  531. * \return zero on success or a negative value on error.
  532. */
  533. int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
  534. struct drm_file *file_priv)
  535. {
  536. struct drm_map *request = data;
  537. struct drm_local_map *map = NULL;
  538. struct drm_map_list *r_list;
  539. int ret;
  540. if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
  541. !drm_core_check_feature(dev, DRIVER_LEGACY))
  542. return -EINVAL;
  543. mutex_lock(&dev->struct_mutex);
  544. list_for_each_entry(r_list, &dev->maplist, head) {
  545. if (r_list->map &&
  546. r_list->user_token == (unsigned long)request->handle &&
  547. r_list->map->flags & _DRM_REMOVABLE) {
  548. map = r_list->map;
  549. break;
  550. }
  551. }
  552. /* List has wrapped around to the head pointer, or its empty we didn't
  553. * find anything.
  554. */
  555. if (list_empty(&dev->maplist) || !map) {
  556. mutex_unlock(&dev->struct_mutex);
  557. return -EINVAL;
  558. }
  559. /* Register and framebuffer maps are permanent */
  560. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  561. mutex_unlock(&dev->struct_mutex);
  562. return 0;
  563. }
  564. ret = drm_legacy_rmmap_locked(dev, map);
  565. mutex_unlock(&dev->struct_mutex);
  566. return ret;
  567. }
  568. /**
  569. * Cleanup after an error on one of the addbufs() functions.
  570. *
  571. * \param dev DRM device.
  572. * \param entry buffer entry where the error occurred.
  573. *
  574. * Frees any pages and buffers associated with the given entry.
  575. */
  576. static void drm_cleanup_buf_error(struct drm_device *dev,
  577. struct drm_buf_entry *entry)
  578. {
  579. int i;
  580. if (entry->seg_count) {
  581. for (i = 0; i < entry->seg_count; i++) {
  582. if (entry->seglist[i]) {
  583. drm_pci_free(dev, entry->seglist[i]);
  584. }
  585. }
  586. kfree(entry->seglist);
  587. entry->seg_count = 0;
  588. }
  589. if (entry->buf_count) {
  590. for (i = 0; i < entry->buf_count; i++) {
  591. kfree(entry->buflist[i].dev_private);
  592. }
  593. kfree(entry->buflist);
  594. entry->buf_count = 0;
  595. }
  596. }
  597. #if IS_ENABLED(CONFIG_AGP)
  598. /**
  599. * Add AGP buffers for DMA transfers.
  600. *
  601. * \param dev struct drm_device to which the buffers are to be added.
  602. * \param request pointer to a struct drm_buf_desc describing the request.
  603. * \return zero on success or a negative number on failure.
  604. *
  605. * After some sanity checks creates a drm_buf structure for each buffer and
  606. * reallocates the buffer list of the same size order to accommodate the new
  607. * buffers.
  608. */
  609. int drm_legacy_addbufs_agp(struct drm_device *dev,
  610. struct drm_buf_desc *request)
  611. {
  612. struct drm_device_dma *dma = dev->dma;
  613. struct drm_buf_entry *entry;
  614. struct drm_agp_mem *agp_entry;
  615. struct drm_buf *buf;
  616. unsigned long offset;
  617. unsigned long agp_offset;
  618. int count;
  619. int order;
  620. int size;
  621. int alignment;
  622. int page_order;
  623. int total;
  624. int byte_count;
  625. int i, valid;
  626. struct drm_buf **temp_buflist;
  627. if (!dma)
  628. return -EINVAL;
  629. count = request->count;
  630. order = order_base_2(request->size);
  631. size = 1 << order;
  632. alignment = (request->flags & _DRM_PAGE_ALIGN)
  633. ? PAGE_ALIGN(size) : size;
  634. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  635. total = PAGE_SIZE << page_order;
  636. byte_count = 0;
  637. agp_offset = dev->agp->base + request->agp_start;
  638. DRM_DEBUG("count: %d\n", count);
  639. DRM_DEBUG("order: %d\n", order);
  640. DRM_DEBUG("size: %d\n", size);
  641. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  642. DRM_DEBUG("alignment: %d\n", alignment);
  643. DRM_DEBUG("page_order: %d\n", page_order);
  644. DRM_DEBUG("total: %d\n", total);
  645. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  646. return -EINVAL;
  647. /* Make sure buffers are located in AGP memory that we own */
  648. valid = 0;
  649. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  650. if ((agp_offset >= agp_entry->bound) &&
  651. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  652. valid = 1;
  653. break;
  654. }
  655. }
  656. if (!list_empty(&dev->agp->memory) && !valid) {
  657. DRM_DEBUG("zone invalid\n");
  658. return -EINVAL;
  659. }
  660. spin_lock(&dev->buf_lock);
  661. if (dev->buf_use) {
  662. spin_unlock(&dev->buf_lock);
  663. return -EBUSY;
  664. }
  665. atomic_inc(&dev->buf_alloc);
  666. spin_unlock(&dev->buf_lock);
  667. mutex_lock(&dev->struct_mutex);
  668. entry = &dma->bufs[order];
  669. if (entry->buf_count) {
  670. mutex_unlock(&dev->struct_mutex);
  671. atomic_dec(&dev->buf_alloc);
  672. return -ENOMEM; /* May only call once for each order */
  673. }
  674. if (count < 0 || count > 4096) {
  675. mutex_unlock(&dev->struct_mutex);
  676. atomic_dec(&dev->buf_alloc);
  677. return -EINVAL;
  678. }
  679. entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
  680. if (!entry->buflist) {
  681. mutex_unlock(&dev->struct_mutex);
  682. atomic_dec(&dev->buf_alloc);
  683. return -ENOMEM;
  684. }
  685. entry->buf_size = size;
  686. entry->page_order = page_order;
  687. offset = 0;
  688. while (entry->buf_count < count) {
  689. buf = &entry->buflist[entry->buf_count];
  690. buf->idx = dma->buf_count + entry->buf_count;
  691. buf->total = alignment;
  692. buf->order = order;
  693. buf->used = 0;
  694. buf->offset = (dma->byte_count + offset);
  695. buf->bus_address = agp_offset + offset;
  696. buf->address = (void *)(agp_offset + offset);
  697. buf->next = NULL;
  698. buf->waiting = 0;
  699. buf->pending = 0;
  700. buf->file_priv = NULL;
  701. buf->dev_priv_size = dev->driver->dev_priv_size;
  702. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  703. if (!buf->dev_private) {
  704. /* Set count correctly so we free the proper amount. */
  705. entry->buf_count = count;
  706. drm_cleanup_buf_error(dev, entry);
  707. mutex_unlock(&dev->struct_mutex);
  708. atomic_dec(&dev->buf_alloc);
  709. return -ENOMEM;
  710. }
  711. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  712. offset += alignment;
  713. entry->buf_count++;
  714. byte_count += PAGE_SIZE << page_order;
  715. }
  716. DRM_DEBUG("byte_count: %d\n", byte_count);
  717. temp_buflist = krealloc(dma->buflist,
  718. (dma->buf_count + entry->buf_count) *
  719. sizeof(*dma->buflist), GFP_KERNEL);
  720. if (!temp_buflist) {
  721. /* Free the entry because it isn't valid */
  722. drm_cleanup_buf_error(dev, entry);
  723. mutex_unlock(&dev->struct_mutex);
  724. atomic_dec(&dev->buf_alloc);
  725. return -ENOMEM;
  726. }
  727. dma->buflist = temp_buflist;
  728. for (i = 0; i < entry->buf_count; i++) {
  729. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  730. }
  731. dma->buf_count += entry->buf_count;
  732. dma->seg_count += entry->seg_count;
  733. dma->page_count += byte_count >> PAGE_SHIFT;
  734. dma->byte_count += byte_count;
  735. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  736. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  737. mutex_unlock(&dev->struct_mutex);
  738. request->count = entry->buf_count;
  739. request->size = size;
  740. dma->flags = _DRM_DMA_USE_AGP;
  741. atomic_dec(&dev->buf_alloc);
  742. return 0;
  743. }
  744. EXPORT_SYMBOL(drm_legacy_addbufs_agp);
  745. #endif /* CONFIG_AGP */
  746. int drm_legacy_addbufs_pci(struct drm_device *dev,
  747. struct drm_buf_desc *request)
  748. {
  749. struct drm_device_dma *dma = dev->dma;
  750. int count;
  751. int order;
  752. int size;
  753. int total;
  754. int page_order;
  755. struct drm_buf_entry *entry;
  756. drm_dma_handle_t *dmah;
  757. struct drm_buf *buf;
  758. int alignment;
  759. unsigned long offset;
  760. int i;
  761. int byte_count;
  762. int page_count;
  763. unsigned long *temp_pagelist;
  764. struct drm_buf **temp_buflist;
  765. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  766. return -EINVAL;
  767. if (!dma)
  768. return -EINVAL;
  769. if (!capable(CAP_SYS_ADMIN))
  770. return -EPERM;
  771. count = request->count;
  772. order = order_base_2(request->size);
  773. size = 1 << order;
  774. DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
  775. request->count, request->size, size, order);
  776. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  777. return -EINVAL;
  778. alignment = (request->flags & _DRM_PAGE_ALIGN)
  779. ? PAGE_ALIGN(size) : size;
  780. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  781. total = PAGE_SIZE << page_order;
  782. spin_lock(&dev->buf_lock);
  783. if (dev->buf_use) {
  784. spin_unlock(&dev->buf_lock);
  785. return -EBUSY;
  786. }
  787. atomic_inc(&dev->buf_alloc);
  788. spin_unlock(&dev->buf_lock);
  789. mutex_lock(&dev->struct_mutex);
  790. entry = &dma->bufs[order];
  791. if (entry->buf_count) {
  792. mutex_unlock(&dev->struct_mutex);
  793. atomic_dec(&dev->buf_alloc);
  794. return -ENOMEM; /* May only call once for each order */
  795. }
  796. if (count < 0 || count > 4096) {
  797. mutex_unlock(&dev->struct_mutex);
  798. atomic_dec(&dev->buf_alloc);
  799. return -EINVAL;
  800. }
  801. entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
  802. if (!entry->buflist) {
  803. mutex_unlock(&dev->struct_mutex);
  804. atomic_dec(&dev->buf_alloc);
  805. return -ENOMEM;
  806. }
  807. entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
  808. if (!entry->seglist) {
  809. kfree(entry->buflist);
  810. mutex_unlock(&dev->struct_mutex);
  811. atomic_dec(&dev->buf_alloc);
  812. return -ENOMEM;
  813. }
  814. /* Keep the original pagelist until we know all the allocations
  815. * have succeeded
  816. */
  817. temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
  818. sizeof(*dma->pagelist),
  819. GFP_KERNEL);
  820. if (!temp_pagelist) {
  821. kfree(entry->buflist);
  822. kfree(entry->seglist);
  823. mutex_unlock(&dev->struct_mutex);
  824. atomic_dec(&dev->buf_alloc);
  825. return -ENOMEM;
  826. }
  827. memcpy(temp_pagelist,
  828. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  829. DRM_DEBUG("pagelist: %d entries\n",
  830. dma->page_count + (count << page_order));
  831. entry->buf_size = size;
  832. entry->page_order = page_order;
  833. byte_count = 0;
  834. page_count = 0;
  835. while (entry->buf_count < count) {
  836. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  837. if (!dmah) {
  838. /* Set count correctly so we free the proper amount. */
  839. entry->buf_count = count;
  840. entry->seg_count = count;
  841. drm_cleanup_buf_error(dev, entry);
  842. kfree(temp_pagelist);
  843. mutex_unlock(&dev->struct_mutex);
  844. atomic_dec(&dev->buf_alloc);
  845. return -ENOMEM;
  846. }
  847. entry->seglist[entry->seg_count++] = dmah;
  848. for (i = 0; i < (1 << page_order); i++) {
  849. DRM_DEBUG("page %d @ 0x%08lx\n",
  850. dma->page_count + page_count,
  851. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  852. temp_pagelist[dma->page_count + page_count++]
  853. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  854. }
  855. for (offset = 0;
  856. offset + size <= total && entry->buf_count < count;
  857. offset += alignment, ++entry->buf_count) {
  858. buf = &entry->buflist[entry->buf_count];
  859. buf->idx = dma->buf_count + entry->buf_count;
  860. buf->total = alignment;
  861. buf->order = order;
  862. buf->used = 0;
  863. buf->offset = (dma->byte_count + byte_count + offset);
  864. buf->address = (void *)(dmah->vaddr + offset);
  865. buf->bus_address = dmah->busaddr + offset;
  866. buf->next = NULL;
  867. buf->waiting = 0;
  868. buf->pending = 0;
  869. buf->file_priv = NULL;
  870. buf->dev_priv_size = dev->driver->dev_priv_size;
  871. buf->dev_private = kzalloc(buf->dev_priv_size,
  872. GFP_KERNEL);
  873. if (!buf->dev_private) {
  874. /* Set count correctly so we free the proper amount. */
  875. entry->buf_count = count;
  876. entry->seg_count = count;
  877. drm_cleanup_buf_error(dev, entry);
  878. kfree(temp_pagelist);
  879. mutex_unlock(&dev->struct_mutex);
  880. atomic_dec(&dev->buf_alloc);
  881. return -ENOMEM;
  882. }
  883. DRM_DEBUG("buffer %d @ %p\n",
  884. entry->buf_count, buf->address);
  885. }
  886. byte_count += PAGE_SIZE << page_order;
  887. }
  888. temp_buflist = krealloc(dma->buflist,
  889. (dma->buf_count + entry->buf_count) *
  890. sizeof(*dma->buflist), GFP_KERNEL);
  891. if (!temp_buflist) {
  892. /* Free the entry because it isn't valid */
  893. drm_cleanup_buf_error(dev, entry);
  894. kfree(temp_pagelist);
  895. mutex_unlock(&dev->struct_mutex);
  896. atomic_dec(&dev->buf_alloc);
  897. return -ENOMEM;
  898. }
  899. dma->buflist = temp_buflist;
  900. for (i = 0; i < entry->buf_count; i++) {
  901. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  902. }
  903. /* No allocations failed, so now we can replace the original pagelist
  904. * with the new one.
  905. */
  906. if (dma->page_count) {
  907. kfree(dma->pagelist);
  908. }
  909. dma->pagelist = temp_pagelist;
  910. dma->buf_count += entry->buf_count;
  911. dma->seg_count += entry->seg_count;
  912. dma->page_count += entry->seg_count << page_order;
  913. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  914. mutex_unlock(&dev->struct_mutex);
  915. request->count = entry->buf_count;
  916. request->size = size;
  917. if (request->flags & _DRM_PCI_BUFFER_RO)
  918. dma->flags = _DRM_DMA_USE_PCI_RO;
  919. atomic_dec(&dev->buf_alloc);
  920. return 0;
  921. }
  922. EXPORT_SYMBOL(drm_legacy_addbufs_pci);
  923. static int drm_legacy_addbufs_sg(struct drm_device *dev,
  924. struct drm_buf_desc *request)
  925. {
  926. struct drm_device_dma *dma = dev->dma;
  927. struct drm_buf_entry *entry;
  928. struct drm_buf *buf;
  929. unsigned long offset;
  930. unsigned long agp_offset;
  931. int count;
  932. int order;
  933. int size;
  934. int alignment;
  935. int page_order;
  936. int total;
  937. int byte_count;
  938. int i;
  939. struct drm_buf **temp_buflist;
  940. if (!drm_core_check_feature(dev, DRIVER_SG))
  941. return -EINVAL;
  942. if (!dma)
  943. return -EINVAL;
  944. if (!capable(CAP_SYS_ADMIN))
  945. return -EPERM;
  946. count = request->count;
  947. order = order_base_2(request->size);
  948. size = 1 << order;
  949. alignment = (request->flags & _DRM_PAGE_ALIGN)
  950. ? PAGE_ALIGN(size) : size;
  951. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  952. total = PAGE_SIZE << page_order;
  953. byte_count = 0;
  954. agp_offset = request->agp_start;
  955. DRM_DEBUG("count: %d\n", count);
  956. DRM_DEBUG("order: %d\n", order);
  957. DRM_DEBUG("size: %d\n", size);
  958. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  959. DRM_DEBUG("alignment: %d\n", alignment);
  960. DRM_DEBUG("page_order: %d\n", page_order);
  961. DRM_DEBUG("total: %d\n", total);
  962. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  963. return -EINVAL;
  964. spin_lock(&dev->buf_lock);
  965. if (dev->buf_use) {
  966. spin_unlock(&dev->buf_lock);
  967. return -EBUSY;
  968. }
  969. atomic_inc(&dev->buf_alloc);
  970. spin_unlock(&dev->buf_lock);
  971. mutex_lock(&dev->struct_mutex);
  972. entry = &dma->bufs[order];
  973. if (entry->buf_count) {
  974. mutex_unlock(&dev->struct_mutex);
  975. atomic_dec(&dev->buf_alloc);
  976. return -ENOMEM; /* May only call once for each order */
  977. }
  978. if (count < 0 || count > 4096) {
  979. mutex_unlock(&dev->struct_mutex);
  980. atomic_dec(&dev->buf_alloc);
  981. return -EINVAL;
  982. }
  983. entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
  984. if (!entry->buflist) {
  985. mutex_unlock(&dev->struct_mutex);
  986. atomic_dec(&dev->buf_alloc);
  987. return -ENOMEM;
  988. }
  989. entry->buf_size = size;
  990. entry->page_order = page_order;
  991. offset = 0;
  992. while (entry->buf_count < count) {
  993. buf = &entry->buflist[entry->buf_count];
  994. buf->idx = dma->buf_count + entry->buf_count;
  995. buf->total = alignment;
  996. buf->order = order;
  997. buf->used = 0;
  998. buf->offset = (dma->byte_count + offset);
  999. buf->bus_address = agp_offset + offset;
  1000. buf->address = (void *)(agp_offset + offset
  1001. + (unsigned long)dev->sg->virtual);
  1002. buf->next = NULL;
  1003. buf->waiting = 0;
  1004. buf->pending = 0;
  1005. buf->file_priv = NULL;
  1006. buf->dev_priv_size = dev->driver->dev_priv_size;
  1007. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  1008. if (!buf->dev_private) {
  1009. /* Set count correctly so we free the proper amount. */
  1010. entry->buf_count = count;
  1011. drm_cleanup_buf_error(dev, entry);
  1012. mutex_unlock(&dev->struct_mutex);
  1013. atomic_dec(&dev->buf_alloc);
  1014. return -ENOMEM;
  1015. }
  1016. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  1017. offset += alignment;
  1018. entry->buf_count++;
  1019. byte_count += PAGE_SIZE << page_order;
  1020. }
  1021. DRM_DEBUG("byte_count: %d\n", byte_count);
  1022. temp_buflist = krealloc(dma->buflist,
  1023. (dma->buf_count + entry->buf_count) *
  1024. sizeof(*dma->buflist), GFP_KERNEL);
  1025. if (!temp_buflist) {
  1026. /* Free the entry because it isn't valid */
  1027. drm_cleanup_buf_error(dev, entry);
  1028. mutex_unlock(&dev->struct_mutex);
  1029. atomic_dec(&dev->buf_alloc);
  1030. return -ENOMEM;
  1031. }
  1032. dma->buflist = temp_buflist;
  1033. for (i = 0; i < entry->buf_count; i++) {
  1034. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  1035. }
  1036. dma->buf_count += entry->buf_count;
  1037. dma->seg_count += entry->seg_count;
  1038. dma->page_count += byte_count >> PAGE_SHIFT;
  1039. dma->byte_count += byte_count;
  1040. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  1041. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  1042. mutex_unlock(&dev->struct_mutex);
  1043. request->count = entry->buf_count;
  1044. request->size = size;
  1045. dma->flags = _DRM_DMA_USE_SG;
  1046. atomic_dec(&dev->buf_alloc);
  1047. return 0;
  1048. }
  1049. /**
  1050. * Add buffers for DMA transfers (ioctl).
  1051. *
  1052. * \param inode device inode.
  1053. * \param file_priv DRM file private.
  1054. * \param cmd command.
  1055. * \param arg pointer to a struct drm_buf_desc request.
  1056. * \return zero on success or a negative number on failure.
  1057. *
  1058. * According with the memory type specified in drm_buf_desc::flags and the
  1059. * build options, it dispatches the call either to addbufs_agp(),
  1060. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  1061. * PCI memory respectively.
  1062. */
  1063. int drm_legacy_addbufs(struct drm_device *dev, void *data,
  1064. struct drm_file *file_priv)
  1065. {
  1066. struct drm_buf_desc *request = data;
  1067. int ret;
  1068. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1069. return -EINVAL;
  1070. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1071. return -EINVAL;
  1072. #if IS_ENABLED(CONFIG_AGP)
  1073. if (request->flags & _DRM_AGP_BUFFER)
  1074. ret = drm_legacy_addbufs_agp(dev, request);
  1075. else
  1076. #endif
  1077. if (request->flags & _DRM_SG_BUFFER)
  1078. ret = drm_legacy_addbufs_sg(dev, request);
  1079. else if (request->flags & _DRM_FB_BUFFER)
  1080. ret = -EINVAL;
  1081. else
  1082. ret = drm_legacy_addbufs_pci(dev, request);
  1083. return ret;
  1084. }
  1085. /**
  1086. * Get information about the buffer mappings.
  1087. *
  1088. * This was originally mean for debugging purposes, or by a sophisticated
  1089. * client library to determine how best to use the available buffers (e.g.,
  1090. * large buffers can be used for image transfer).
  1091. *
  1092. * \param inode device inode.
  1093. * \param file_priv DRM file private.
  1094. * \param cmd command.
  1095. * \param arg pointer to a drm_buf_info structure.
  1096. * \return zero on success or a negative number on failure.
  1097. *
  1098. * Increments drm_device::buf_use while holding the drm_device::buf_lock
  1099. * lock, preventing of allocating more buffers after this call. Information
  1100. * about each requested buffer is then copied into user space.
  1101. */
  1102. int __drm_legacy_infobufs(struct drm_device *dev,
  1103. void *data, int *p,
  1104. int (*f)(void *, int, struct drm_buf_entry *))
  1105. {
  1106. struct drm_device_dma *dma = dev->dma;
  1107. int i;
  1108. int count;
  1109. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1110. return -EINVAL;
  1111. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1112. return -EINVAL;
  1113. if (!dma)
  1114. return -EINVAL;
  1115. spin_lock(&dev->buf_lock);
  1116. if (atomic_read(&dev->buf_alloc)) {
  1117. spin_unlock(&dev->buf_lock);
  1118. return -EBUSY;
  1119. }
  1120. ++dev->buf_use; /* Can't allocate more after this call */
  1121. spin_unlock(&dev->buf_lock);
  1122. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1123. if (dma->bufs[i].buf_count)
  1124. ++count;
  1125. }
  1126. DRM_DEBUG("count = %d\n", count);
  1127. if (*p >= count) {
  1128. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1129. struct drm_buf_entry *from = &dma->bufs[i];
  1130. if (from->buf_count) {
  1131. if (f(data, count, from) < 0)
  1132. return -EFAULT;
  1133. DRM_DEBUG("%d %d %d %d %d\n",
  1134. i,
  1135. dma->bufs[i].buf_count,
  1136. dma->bufs[i].buf_size,
  1137. dma->bufs[i].low_mark,
  1138. dma->bufs[i].high_mark);
  1139. ++count;
  1140. }
  1141. }
  1142. }
  1143. *p = count;
  1144. return 0;
  1145. }
  1146. static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
  1147. {
  1148. struct drm_buf_info *request = data;
  1149. struct drm_buf_desc __user *to = &request->list[count];
  1150. struct drm_buf_desc v = {.count = from->buf_count,
  1151. .size = from->buf_size,
  1152. .low_mark = from->low_mark,
  1153. .high_mark = from->high_mark};
  1154. return copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags));
  1155. }
  1156. int drm_legacy_infobufs(struct drm_device *dev, void *data,
  1157. struct drm_file *file_priv)
  1158. {
  1159. struct drm_buf_info *request = data;
  1160. return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
  1161. }
  1162. /**
  1163. * Specifies a low and high water mark for buffer allocation
  1164. *
  1165. * \param inode device inode.
  1166. * \param file_priv DRM file private.
  1167. * \param cmd command.
  1168. * \param arg a pointer to a drm_buf_desc structure.
  1169. * \return zero on success or a negative number on failure.
  1170. *
  1171. * Verifies that the size order is bounded between the admissible orders and
  1172. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1173. *
  1174. * \note This ioctl is deprecated and mostly never used.
  1175. */
  1176. int drm_legacy_markbufs(struct drm_device *dev, void *data,
  1177. struct drm_file *file_priv)
  1178. {
  1179. struct drm_device_dma *dma = dev->dma;
  1180. struct drm_buf_desc *request = data;
  1181. int order;
  1182. struct drm_buf_entry *entry;
  1183. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1184. return -EINVAL;
  1185. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1186. return -EINVAL;
  1187. if (!dma)
  1188. return -EINVAL;
  1189. DRM_DEBUG("%d, %d, %d\n",
  1190. request->size, request->low_mark, request->high_mark);
  1191. order = order_base_2(request->size);
  1192. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1193. return -EINVAL;
  1194. entry = &dma->bufs[order];
  1195. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1196. return -EINVAL;
  1197. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1198. return -EINVAL;
  1199. entry->low_mark = request->low_mark;
  1200. entry->high_mark = request->high_mark;
  1201. return 0;
  1202. }
  1203. /**
  1204. * Unreserve the buffers in list, previously reserved using drmDMA.
  1205. *
  1206. * \param inode device inode.
  1207. * \param file_priv DRM file private.
  1208. * \param cmd command.
  1209. * \param arg pointer to a drm_buf_free structure.
  1210. * \return zero on success or a negative number on failure.
  1211. *
  1212. * Calls free_buffer() for each used buffer.
  1213. * This function is primarily used for debugging.
  1214. */
  1215. int drm_legacy_freebufs(struct drm_device *dev, void *data,
  1216. struct drm_file *file_priv)
  1217. {
  1218. struct drm_device_dma *dma = dev->dma;
  1219. struct drm_buf_free *request = data;
  1220. int i;
  1221. int idx;
  1222. struct drm_buf *buf;
  1223. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1224. return -EINVAL;
  1225. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1226. return -EINVAL;
  1227. if (!dma)
  1228. return -EINVAL;
  1229. DRM_DEBUG("%d\n", request->count);
  1230. for (i = 0; i < request->count; i++) {
  1231. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1232. return -EFAULT;
  1233. if (idx < 0 || idx >= dma->buf_count) {
  1234. DRM_ERROR("Index %d (of %d max)\n",
  1235. idx, dma->buf_count - 1);
  1236. return -EINVAL;
  1237. }
  1238. buf = dma->buflist[idx];
  1239. if (buf->file_priv != file_priv) {
  1240. DRM_ERROR("Process %d freeing buffer not owned\n",
  1241. task_pid_nr(current));
  1242. return -EINVAL;
  1243. }
  1244. drm_legacy_free_buffer(dev, buf);
  1245. }
  1246. return 0;
  1247. }
  1248. /**
  1249. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1250. *
  1251. * \param inode device inode.
  1252. * \param file_priv DRM file private.
  1253. * \param cmd command.
  1254. * \param arg pointer to a drm_buf_map structure.
  1255. * \return zero on success or a negative number on failure.
  1256. *
  1257. * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
  1258. * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
  1259. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1260. * drm_mmap_dma().
  1261. */
  1262. int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
  1263. void __user **v,
  1264. int (*f)(void *, int, unsigned long,
  1265. struct drm_buf *),
  1266. struct drm_file *file_priv)
  1267. {
  1268. struct drm_device_dma *dma = dev->dma;
  1269. int retcode = 0;
  1270. unsigned long virtual;
  1271. int i;
  1272. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1273. return -EINVAL;
  1274. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1275. return -EINVAL;
  1276. if (!dma)
  1277. return -EINVAL;
  1278. spin_lock(&dev->buf_lock);
  1279. if (atomic_read(&dev->buf_alloc)) {
  1280. spin_unlock(&dev->buf_lock);
  1281. return -EBUSY;
  1282. }
  1283. dev->buf_use++; /* Can't allocate more after this call */
  1284. spin_unlock(&dev->buf_lock);
  1285. if (*p >= dma->buf_count) {
  1286. if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
  1287. || (drm_core_check_feature(dev, DRIVER_SG)
  1288. && (dma->flags & _DRM_DMA_USE_SG))) {
  1289. struct drm_local_map *map = dev->agp_buffer_map;
  1290. unsigned long token = dev->agp_buffer_token;
  1291. if (!map) {
  1292. retcode = -EINVAL;
  1293. goto done;
  1294. }
  1295. virtual = vm_mmap(file_priv->filp, 0, map->size,
  1296. PROT_READ | PROT_WRITE,
  1297. MAP_SHARED,
  1298. token);
  1299. } else {
  1300. virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
  1301. PROT_READ | PROT_WRITE,
  1302. MAP_SHARED, 0);
  1303. }
  1304. if (virtual > -1024UL) {
  1305. /* Real error */
  1306. retcode = (signed long)virtual;
  1307. goto done;
  1308. }
  1309. *v = (void __user *)virtual;
  1310. for (i = 0; i < dma->buf_count; i++) {
  1311. if (f(data, i, virtual, dma->buflist[i]) < 0) {
  1312. retcode = -EFAULT;
  1313. goto done;
  1314. }
  1315. }
  1316. }
  1317. done:
  1318. *p = dma->buf_count;
  1319. DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
  1320. return retcode;
  1321. }
  1322. static int map_one_buf(void *data, int idx, unsigned long virtual,
  1323. struct drm_buf *buf)
  1324. {
  1325. struct drm_buf_map *request = data;
  1326. unsigned long address = virtual + buf->offset; /* *** */
  1327. if (copy_to_user(&request->list[idx].idx, &buf->idx,
  1328. sizeof(request->list[0].idx)))
  1329. return -EFAULT;
  1330. if (copy_to_user(&request->list[idx].total, &buf->total,
  1331. sizeof(request->list[0].total)))
  1332. return -EFAULT;
  1333. if (clear_user(&request->list[idx].used, sizeof(int)))
  1334. return -EFAULT;
  1335. if (copy_to_user(&request->list[idx].address, &address,
  1336. sizeof(address)))
  1337. return -EFAULT;
  1338. return 0;
  1339. }
  1340. int drm_legacy_mapbufs(struct drm_device *dev, void *data,
  1341. struct drm_file *file_priv)
  1342. {
  1343. struct drm_buf_map *request = data;
  1344. return __drm_legacy_mapbufs(dev, data, &request->count,
  1345. &request->virtual, map_one_buf,
  1346. file_priv);
  1347. }
  1348. int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
  1349. struct drm_file *file_priv)
  1350. {
  1351. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1352. return -EINVAL;
  1353. if (dev->driver->dma_ioctl)
  1354. return dev->driver->dma_ioctl(dev, data, file_priv);
  1355. else
  1356. return -EINVAL;
  1357. }
  1358. struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
  1359. {
  1360. struct drm_map_list *entry;
  1361. list_for_each_entry(entry, &dev->maplist, head) {
  1362. if (entry->map && entry->map->type == _DRM_SHM &&
  1363. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  1364. return entry->map;
  1365. }
  1366. }
  1367. return NULL;
  1368. }
  1369. EXPORT_SYMBOL(drm_legacy_getsarea);