drm_bufs.c 41 KB

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