drm_bufs.c 42 KB

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