drm_bufs.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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->primary->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->primary->master->lock.hw_lock != NULL) {
  230. vfree(map->handle);
  231. kfree(map);
  232. return -EBUSY;
  233. }
  234. dev->sigdata.lock = dev->primary->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->primary->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. err = drm_addmap_core(dev, map->offset, map->size, map->type,
  368. map->flags, &maplist);
  369. if (err)
  370. return err;
  371. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  372. map->handle = (void *)(unsigned long)maplist->user_token;
  373. /*
  374. * It appears that there are no users of this value whatsoever --
  375. * drmAddMap just discards it. Let's not encourage its use.
  376. * (Keeping drm_addmap_core's returned mtrr value would be wrong --
  377. * it's not a real mtrr index anymore.)
  378. */
  379. map->mtrr = -1;
  380. return 0;
  381. }
  382. /**
  383. * Remove a map private from list and deallocate resources if the mapping
  384. * isn't in use.
  385. *
  386. * Searches the map on drm_device::maplist, removes it from the list, see if
  387. * its being used, and free any associate resource (such as MTRR's) if it's not
  388. * being on use.
  389. *
  390. * \sa drm_legacy_addmap
  391. */
  392. int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  393. {
  394. struct drm_map_list *r_list = NULL, *list_t;
  395. drm_dma_handle_t dmah;
  396. int found = 0;
  397. struct drm_master *master;
  398. /* Find the list entry for the map and remove it */
  399. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  400. if (r_list->map == map) {
  401. master = r_list->master;
  402. list_del(&r_list->head);
  403. drm_ht_remove_key(&dev->map_hash,
  404. r_list->user_token >> PAGE_SHIFT);
  405. kfree(r_list);
  406. found = 1;
  407. break;
  408. }
  409. }
  410. if (!found)
  411. return -EINVAL;
  412. switch (map->type) {
  413. case _DRM_REGISTERS:
  414. iounmap(map->handle);
  415. /* FALLTHROUGH */
  416. case _DRM_FRAME_BUFFER:
  417. arch_phys_wc_del(map->mtrr);
  418. break;
  419. case _DRM_SHM:
  420. vfree(map->handle);
  421. if (master) {
  422. if (dev->sigdata.lock == master->lock.hw_lock)
  423. dev->sigdata.lock = NULL;
  424. master->lock.hw_lock = NULL; /* SHM removed */
  425. master->lock.file_priv = NULL;
  426. wake_up_interruptible_all(&master->lock.lock_queue);
  427. }
  428. break;
  429. case _DRM_AGP:
  430. case _DRM_SCATTER_GATHER:
  431. break;
  432. case _DRM_CONSISTENT:
  433. dmah.vaddr = map->handle;
  434. dmah.busaddr = map->offset;
  435. dmah.size = map->size;
  436. __drm_legacy_pci_free(dev, &dmah);
  437. break;
  438. }
  439. kfree(map);
  440. return 0;
  441. }
  442. EXPORT_SYMBOL(drm_legacy_rmmap_locked);
  443. int drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
  444. {
  445. int ret;
  446. mutex_lock(&dev->struct_mutex);
  447. ret = drm_legacy_rmmap_locked(dev, map);
  448. mutex_unlock(&dev->struct_mutex);
  449. return ret;
  450. }
  451. EXPORT_SYMBOL(drm_legacy_rmmap);
  452. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  453. * the last close of the device, and this is necessary for cleanup when things
  454. * exit uncleanly. Therefore, having userland manually remove mappings seems
  455. * like a pointless exercise since they're going away anyway.
  456. *
  457. * One use case might be after addmap is allowed for normal users for SHM and
  458. * gets used by drivers that the server doesn't need to care about. This seems
  459. * unlikely.
  460. *
  461. * \param inode device inode.
  462. * \param file_priv DRM file private.
  463. * \param cmd command.
  464. * \param arg pointer to a struct drm_map structure.
  465. * \return zero on success or a negative value on error.
  466. */
  467. int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
  468. struct drm_file *file_priv)
  469. {
  470. struct drm_map *request = data;
  471. struct drm_local_map *map = NULL;
  472. struct drm_map_list *r_list;
  473. int ret;
  474. mutex_lock(&dev->struct_mutex);
  475. list_for_each_entry(r_list, &dev->maplist, head) {
  476. if (r_list->map &&
  477. r_list->user_token == (unsigned long)request->handle &&
  478. r_list->map->flags & _DRM_REMOVABLE) {
  479. map = r_list->map;
  480. break;
  481. }
  482. }
  483. /* List has wrapped around to the head pointer, or its empty we didn't
  484. * find anything.
  485. */
  486. if (list_empty(&dev->maplist) || !map) {
  487. mutex_unlock(&dev->struct_mutex);
  488. return -EINVAL;
  489. }
  490. /* Register and framebuffer maps are permanent */
  491. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  492. mutex_unlock(&dev->struct_mutex);
  493. return 0;
  494. }
  495. ret = drm_legacy_rmmap_locked(dev, map);
  496. mutex_unlock(&dev->struct_mutex);
  497. return ret;
  498. }
  499. /**
  500. * Cleanup after an error on one of the addbufs() functions.
  501. *
  502. * \param dev DRM device.
  503. * \param entry buffer entry where the error occurred.
  504. *
  505. * Frees any pages and buffers associated with the given entry.
  506. */
  507. static void drm_cleanup_buf_error(struct drm_device * dev,
  508. struct drm_buf_entry * entry)
  509. {
  510. int i;
  511. if (entry->seg_count) {
  512. for (i = 0; i < entry->seg_count; i++) {
  513. if (entry->seglist[i]) {
  514. drm_pci_free(dev, entry->seglist[i]);
  515. }
  516. }
  517. kfree(entry->seglist);
  518. entry->seg_count = 0;
  519. }
  520. if (entry->buf_count) {
  521. for (i = 0; i < entry->buf_count; i++) {
  522. kfree(entry->buflist[i].dev_private);
  523. }
  524. kfree(entry->buflist);
  525. entry->buf_count = 0;
  526. }
  527. }
  528. #if IS_ENABLED(CONFIG_AGP)
  529. /**
  530. * Add AGP buffers for DMA transfers.
  531. *
  532. * \param dev struct drm_device to which the buffers are to be added.
  533. * \param request pointer to a struct drm_buf_desc describing the request.
  534. * \return zero on success or a negative number on failure.
  535. *
  536. * After some sanity checks creates a drm_buf structure for each buffer and
  537. * reallocates the buffer list of the same size order to accommodate the new
  538. * buffers.
  539. */
  540. int drm_legacy_addbufs_agp(struct drm_device *dev,
  541. struct drm_buf_desc *request)
  542. {
  543. struct drm_device_dma *dma = dev->dma;
  544. struct drm_buf_entry *entry;
  545. struct drm_agp_mem *agp_entry;
  546. struct drm_buf *buf;
  547. unsigned long offset;
  548. unsigned long agp_offset;
  549. int count;
  550. int order;
  551. int size;
  552. int alignment;
  553. int page_order;
  554. int total;
  555. int byte_count;
  556. int i, valid;
  557. struct drm_buf **temp_buflist;
  558. if (!dma)
  559. return -EINVAL;
  560. count = request->count;
  561. order = order_base_2(request->size);
  562. size = 1 << order;
  563. alignment = (request->flags & _DRM_PAGE_ALIGN)
  564. ? PAGE_ALIGN(size) : size;
  565. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  566. total = PAGE_SIZE << page_order;
  567. byte_count = 0;
  568. agp_offset = dev->agp->base + request->agp_start;
  569. DRM_DEBUG("count: %d\n", count);
  570. DRM_DEBUG("order: %d\n", order);
  571. DRM_DEBUG("size: %d\n", size);
  572. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  573. DRM_DEBUG("alignment: %d\n", alignment);
  574. DRM_DEBUG("page_order: %d\n", page_order);
  575. DRM_DEBUG("total: %d\n", total);
  576. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  577. return -EINVAL;
  578. /* Make sure buffers are located in AGP memory that we own */
  579. valid = 0;
  580. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  581. if ((agp_offset >= agp_entry->bound) &&
  582. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  583. valid = 1;
  584. break;
  585. }
  586. }
  587. if (!list_empty(&dev->agp->memory) && !valid) {
  588. DRM_DEBUG("zone invalid\n");
  589. return -EINVAL;
  590. }
  591. spin_lock(&dev->buf_lock);
  592. if (dev->buf_use) {
  593. spin_unlock(&dev->buf_lock);
  594. return -EBUSY;
  595. }
  596. atomic_inc(&dev->buf_alloc);
  597. spin_unlock(&dev->buf_lock);
  598. mutex_lock(&dev->struct_mutex);
  599. entry = &dma->bufs[order];
  600. if (entry->buf_count) {
  601. mutex_unlock(&dev->struct_mutex);
  602. atomic_dec(&dev->buf_alloc);
  603. return -ENOMEM; /* May only call once for each order */
  604. }
  605. if (count < 0 || count > 4096) {
  606. mutex_unlock(&dev->struct_mutex);
  607. atomic_dec(&dev->buf_alloc);
  608. return -EINVAL;
  609. }
  610. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  611. if (!entry->buflist) {
  612. mutex_unlock(&dev->struct_mutex);
  613. atomic_dec(&dev->buf_alloc);
  614. return -ENOMEM;
  615. }
  616. entry->buf_size = size;
  617. entry->page_order = page_order;
  618. offset = 0;
  619. while (entry->buf_count < count) {
  620. buf = &entry->buflist[entry->buf_count];
  621. buf->idx = dma->buf_count + entry->buf_count;
  622. buf->total = alignment;
  623. buf->order = order;
  624. buf->used = 0;
  625. buf->offset = (dma->byte_count + offset);
  626. buf->bus_address = agp_offset + offset;
  627. buf->address = (void *)(agp_offset + offset);
  628. buf->next = NULL;
  629. buf->waiting = 0;
  630. buf->pending = 0;
  631. buf->file_priv = NULL;
  632. buf->dev_priv_size = dev->driver->dev_priv_size;
  633. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  634. if (!buf->dev_private) {
  635. /* Set count correctly so we free the proper amount. */
  636. entry->buf_count = count;
  637. drm_cleanup_buf_error(dev, entry);
  638. mutex_unlock(&dev->struct_mutex);
  639. atomic_dec(&dev->buf_alloc);
  640. return -ENOMEM;
  641. }
  642. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  643. offset += alignment;
  644. entry->buf_count++;
  645. byte_count += PAGE_SIZE << page_order;
  646. }
  647. DRM_DEBUG("byte_count: %d\n", byte_count);
  648. temp_buflist = krealloc(dma->buflist,
  649. (dma->buf_count + entry->buf_count) *
  650. sizeof(*dma->buflist), GFP_KERNEL);
  651. if (!temp_buflist) {
  652. /* Free the entry because it isn't valid */
  653. drm_cleanup_buf_error(dev, entry);
  654. mutex_unlock(&dev->struct_mutex);
  655. atomic_dec(&dev->buf_alloc);
  656. return -ENOMEM;
  657. }
  658. dma->buflist = temp_buflist;
  659. for (i = 0; i < entry->buf_count; i++) {
  660. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  661. }
  662. dma->buf_count += entry->buf_count;
  663. dma->seg_count += entry->seg_count;
  664. dma->page_count += byte_count >> PAGE_SHIFT;
  665. dma->byte_count += byte_count;
  666. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  667. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  668. mutex_unlock(&dev->struct_mutex);
  669. request->count = entry->buf_count;
  670. request->size = size;
  671. dma->flags = _DRM_DMA_USE_AGP;
  672. atomic_dec(&dev->buf_alloc);
  673. return 0;
  674. }
  675. EXPORT_SYMBOL(drm_legacy_addbufs_agp);
  676. #endif /* CONFIG_AGP */
  677. int drm_legacy_addbufs_pci(struct drm_device *dev,
  678. struct drm_buf_desc *request)
  679. {
  680. struct drm_device_dma *dma = dev->dma;
  681. int count;
  682. int order;
  683. int size;
  684. int total;
  685. int page_order;
  686. struct drm_buf_entry *entry;
  687. drm_dma_handle_t *dmah;
  688. struct drm_buf *buf;
  689. int alignment;
  690. unsigned long offset;
  691. int i;
  692. int byte_count;
  693. int page_count;
  694. unsigned long *temp_pagelist;
  695. struct drm_buf **temp_buflist;
  696. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  697. return -EINVAL;
  698. if (!dma)
  699. return -EINVAL;
  700. if (!capable(CAP_SYS_ADMIN))
  701. return -EPERM;
  702. count = request->count;
  703. order = order_base_2(request->size);
  704. size = 1 << order;
  705. DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
  706. request->count, request->size, size, order);
  707. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  708. return -EINVAL;
  709. alignment = (request->flags & _DRM_PAGE_ALIGN)
  710. ? PAGE_ALIGN(size) : size;
  711. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  712. total = PAGE_SIZE << page_order;
  713. spin_lock(&dev->buf_lock);
  714. if (dev->buf_use) {
  715. spin_unlock(&dev->buf_lock);
  716. return -EBUSY;
  717. }
  718. atomic_inc(&dev->buf_alloc);
  719. spin_unlock(&dev->buf_lock);
  720. mutex_lock(&dev->struct_mutex);
  721. entry = &dma->bufs[order];
  722. if (entry->buf_count) {
  723. mutex_unlock(&dev->struct_mutex);
  724. atomic_dec(&dev->buf_alloc);
  725. return -ENOMEM; /* May only call once for each order */
  726. }
  727. if (count < 0 || count > 4096) {
  728. mutex_unlock(&dev->struct_mutex);
  729. atomic_dec(&dev->buf_alloc);
  730. return -EINVAL;
  731. }
  732. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  733. if (!entry->buflist) {
  734. mutex_unlock(&dev->struct_mutex);
  735. atomic_dec(&dev->buf_alloc);
  736. return -ENOMEM;
  737. }
  738. entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
  739. if (!entry->seglist) {
  740. kfree(entry->buflist);
  741. mutex_unlock(&dev->struct_mutex);
  742. atomic_dec(&dev->buf_alloc);
  743. return -ENOMEM;
  744. }
  745. /* Keep the original pagelist until we know all the allocations
  746. * have succeeded
  747. */
  748. temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
  749. sizeof(*dma->pagelist), GFP_KERNEL);
  750. if (!temp_pagelist) {
  751. kfree(entry->buflist);
  752. kfree(entry->seglist);
  753. mutex_unlock(&dev->struct_mutex);
  754. atomic_dec(&dev->buf_alloc);
  755. return -ENOMEM;
  756. }
  757. memcpy(temp_pagelist,
  758. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  759. DRM_DEBUG("pagelist: %d entries\n",
  760. dma->page_count + (count << page_order));
  761. entry->buf_size = size;
  762. entry->page_order = page_order;
  763. byte_count = 0;
  764. page_count = 0;
  765. while (entry->buf_count < count) {
  766. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  767. if (!dmah) {
  768. /* Set count correctly so we free the proper amount. */
  769. entry->buf_count = count;
  770. entry->seg_count = count;
  771. drm_cleanup_buf_error(dev, entry);
  772. kfree(temp_pagelist);
  773. mutex_unlock(&dev->struct_mutex);
  774. atomic_dec(&dev->buf_alloc);
  775. return -ENOMEM;
  776. }
  777. entry->seglist[entry->seg_count++] = dmah;
  778. for (i = 0; i < (1 << page_order); i++) {
  779. DRM_DEBUG("page %d @ 0x%08lx\n",
  780. dma->page_count + page_count,
  781. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  782. temp_pagelist[dma->page_count + page_count++]
  783. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  784. }
  785. for (offset = 0;
  786. offset + size <= total && entry->buf_count < count;
  787. offset += alignment, ++entry->buf_count) {
  788. buf = &entry->buflist[entry->buf_count];
  789. buf->idx = dma->buf_count + entry->buf_count;
  790. buf->total = alignment;
  791. buf->order = order;
  792. buf->used = 0;
  793. buf->offset = (dma->byte_count + byte_count + offset);
  794. buf->address = (void *)(dmah->vaddr + offset);
  795. buf->bus_address = dmah->busaddr + offset;
  796. buf->next = NULL;
  797. buf->waiting = 0;
  798. buf->pending = 0;
  799. buf->file_priv = NULL;
  800. buf->dev_priv_size = dev->driver->dev_priv_size;
  801. buf->dev_private = kzalloc(buf->dev_priv_size,
  802. GFP_KERNEL);
  803. if (!buf->dev_private) {
  804. /* Set count correctly so we free the proper amount. */
  805. entry->buf_count = count;
  806. entry->seg_count = count;
  807. drm_cleanup_buf_error(dev, entry);
  808. kfree(temp_pagelist);
  809. mutex_unlock(&dev->struct_mutex);
  810. atomic_dec(&dev->buf_alloc);
  811. return -ENOMEM;
  812. }
  813. DRM_DEBUG("buffer %d @ %p\n",
  814. entry->buf_count, buf->address);
  815. }
  816. byte_count += PAGE_SIZE << page_order;
  817. }
  818. temp_buflist = krealloc(dma->buflist,
  819. (dma->buf_count + entry->buf_count) *
  820. sizeof(*dma->buflist), GFP_KERNEL);
  821. if (!temp_buflist) {
  822. /* Free the entry because it isn't valid */
  823. drm_cleanup_buf_error(dev, entry);
  824. kfree(temp_pagelist);
  825. mutex_unlock(&dev->struct_mutex);
  826. atomic_dec(&dev->buf_alloc);
  827. return -ENOMEM;
  828. }
  829. dma->buflist = temp_buflist;
  830. for (i = 0; i < entry->buf_count; i++) {
  831. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  832. }
  833. /* No allocations failed, so now we can replace the original pagelist
  834. * with the new one.
  835. */
  836. if (dma->page_count) {
  837. kfree(dma->pagelist);
  838. }
  839. dma->pagelist = temp_pagelist;
  840. dma->buf_count += entry->buf_count;
  841. dma->seg_count += entry->seg_count;
  842. dma->page_count += entry->seg_count << page_order;
  843. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  844. mutex_unlock(&dev->struct_mutex);
  845. request->count = entry->buf_count;
  846. request->size = size;
  847. if (request->flags & _DRM_PCI_BUFFER_RO)
  848. dma->flags = _DRM_DMA_USE_PCI_RO;
  849. atomic_dec(&dev->buf_alloc);
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(drm_legacy_addbufs_pci);
  853. static int drm_legacy_addbufs_sg(struct drm_device *dev,
  854. struct drm_buf_desc *request)
  855. {
  856. struct drm_device_dma *dma = dev->dma;
  857. struct drm_buf_entry *entry;
  858. struct drm_buf *buf;
  859. unsigned long offset;
  860. unsigned long agp_offset;
  861. int count;
  862. int order;
  863. int size;
  864. int alignment;
  865. int page_order;
  866. int total;
  867. int byte_count;
  868. int i;
  869. struct drm_buf **temp_buflist;
  870. if (!drm_core_check_feature(dev, DRIVER_SG))
  871. return -EINVAL;
  872. if (!dma)
  873. return -EINVAL;
  874. if (!capable(CAP_SYS_ADMIN))
  875. return -EPERM;
  876. count = request->count;
  877. order = order_base_2(request->size);
  878. size = 1 << order;
  879. alignment = (request->flags & _DRM_PAGE_ALIGN)
  880. ? PAGE_ALIGN(size) : size;
  881. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  882. total = PAGE_SIZE << page_order;
  883. byte_count = 0;
  884. agp_offset = request->agp_start;
  885. DRM_DEBUG("count: %d\n", count);
  886. DRM_DEBUG("order: %d\n", order);
  887. DRM_DEBUG("size: %d\n", size);
  888. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  889. DRM_DEBUG("alignment: %d\n", alignment);
  890. DRM_DEBUG("page_order: %d\n", page_order);
  891. DRM_DEBUG("total: %d\n", total);
  892. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  893. return -EINVAL;
  894. spin_lock(&dev->buf_lock);
  895. if (dev->buf_use) {
  896. spin_unlock(&dev->buf_lock);
  897. return -EBUSY;
  898. }
  899. atomic_inc(&dev->buf_alloc);
  900. spin_unlock(&dev->buf_lock);
  901. mutex_lock(&dev->struct_mutex);
  902. entry = &dma->bufs[order];
  903. if (entry->buf_count) {
  904. mutex_unlock(&dev->struct_mutex);
  905. atomic_dec(&dev->buf_alloc);
  906. return -ENOMEM; /* May only call once for each order */
  907. }
  908. if (count < 0 || count > 4096) {
  909. mutex_unlock(&dev->struct_mutex);
  910. atomic_dec(&dev->buf_alloc);
  911. return -EINVAL;
  912. }
  913. entry->buflist = kzalloc(count * sizeof(*entry->buflist),
  914. GFP_KERNEL);
  915. if (!entry->buflist) {
  916. mutex_unlock(&dev->struct_mutex);
  917. atomic_dec(&dev->buf_alloc);
  918. return -ENOMEM;
  919. }
  920. entry->buf_size = size;
  921. entry->page_order = page_order;
  922. offset = 0;
  923. while (entry->buf_count < count) {
  924. buf = &entry->buflist[entry->buf_count];
  925. buf->idx = dma->buf_count + entry->buf_count;
  926. buf->total = alignment;
  927. buf->order = order;
  928. buf->used = 0;
  929. buf->offset = (dma->byte_count + offset);
  930. buf->bus_address = agp_offset + offset;
  931. buf->address = (void *)(agp_offset + offset
  932. + (unsigned long)dev->sg->virtual);
  933. buf->next = NULL;
  934. buf->waiting = 0;
  935. buf->pending = 0;
  936. buf->file_priv = NULL;
  937. buf->dev_priv_size = dev->driver->dev_priv_size;
  938. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  939. if (!buf->dev_private) {
  940. /* Set count correctly so we free the proper amount. */
  941. entry->buf_count = count;
  942. drm_cleanup_buf_error(dev, entry);
  943. mutex_unlock(&dev->struct_mutex);
  944. atomic_dec(&dev->buf_alloc);
  945. return -ENOMEM;
  946. }
  947. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  948. offset += alignment;
  949. entry->buf_count++;
  950. byte_count += PAGE_SIZE << page_order;
  951. }
  952. DRM_DEBUG("byte_count: %d\n", byte_count);
  953. temp_buflist = krealloc(dma->buflist,
  954. (dma->buf_count + entry->buf_count) *
  955. sizeof(*dma->buflist), GFP_KERNEL);
  956. if (!temp_buflist) {
  957. /* Free the entry because it isn't valid */
  958. drm_cleanup_buf_error(dev, entry);
  959. mutex_unlock(&dev->struct_mutex);
  960. atomic_dec(&dev->buf_alloc);
  961. return -ENOMEM;
  962. }
  963. dma->buflist = temp_buflist;
  964. for (i = 0; i < entry->buf_count; i++) {
  965. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  966. }
  967. dma->buf_count += entry->buf_count;
  968. dma->seg_count += entry->seg_count;
  969. dma->page_count += byte_count >> PAGE_SHIFT;
  970. dma->byte_count += byte_count;
  971. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  972. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  973. mutex_unlock(&dev->struct_mutex);
  974. request->count = entry->buf_count;
  975. request->size = size;
  976. dma->flags = _DRM_DMA_USE_SG;
  977. atomic_dec(&dev->buf_alloc);
  978. return 0;
  979. }
  980. /**
  981. * Add buffers for DMA transfers (ioctl).
  982. *
  983. * \param inode device inode.
  984. * \param file_priv DRM file private.
  985. * \param cmd command.
  986. * \param arg pointer to a struct drm_buf_desc request.
  987. * \return zero on success or a negative number on failure.
  988. *
  989. * According with the memory type specified in drm_buf_desc::flags and the
  990. * build options, it dispatches the call either to addbufs_agp(),
  991. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  992. * PCI memory respectively.
  993. */
  994. int drm_legacy_addbufs(struct drm_device *dev, void *data,
  995. struct drm_file *file_priv)
  996. {
  997. struct drm_buf_desc *request = data;
  998. int ret;
  999. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1000. return -EINVAL;
  1001. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1002. return -EINVAL;
  1003. #if IS_ENABLED(CONFIG_AGP)
  1004. if (request->flags & _DRM_AGP_BUFFER)
  1005. ret = drm_legacy_addbufs_agp(dev, request);
  1006. else
  1007. #endif
  1008. if (request->flags & _DRM_SG_BUFFER)
  1009. ret = drm_legacy_addbufs_sg(dev, request);
  1010. else if (request->flags & _DRM_FB_BUFFER)
  1011. ret = -EINVAL;
  1012. else
  1013. ret = drm_legacy_addbufs_pci(dev, request);
  1014. return ret;
  1015. }
  1016. /**
  1017. * Get information about the buffer mappings.
  1018. *
  1019. * This was originally mean for debugging purposes, or by a sophisticated
  1020. * client library to determine how best to use the available buffers (e.g.,
  1021. * large buffers can be used for image transfer).
  1022. *
  1023. * \param inode device inode.
  1024. * \param file_priv DRM file private.
  1025. * \param cmd command.
  1026. * \param arg pointer to a drm_buf_info structure.
  1027. * \return zero on success or a negative number on failure.
  1028. *
  1029. * Increments drm_device::buf_use while holding the drm_device::buf_lock
  1030. * lock, preventing of allocating more buffers after this call. Information
  1031. * about each requested buffer is then copied into user space.
  1032. */
  1033. int drm_legacy_infobufs(struct drm_device *dev, void *data,
  1034. struct drm_file *file_priv)
  1035. {
  1036. struct drm_device_dma *dma = dev->dma;
  1037. struct drm_buf_info *request = data;
  1038. int i;
  1039. int count;
  1040. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1041. return -EINVAL;
  1042. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1043. return -EINVAL;
  1044. if (!dma)
  1045. return -EINVAL;
  1046. spin_lock(&dev->buf_lock);
  1047. if (atomic_read(&dev->buf_alloc)) {
  1048. spin_unlock(&dev->buf_lock);
  1049. return -EBUSY;
  1050. }
  1051. ++dev->buf_use; /* Can't allocate more after this call */
  1052. spin_unlock(&dev->buf_lock);
  1053. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1054. if (dma->bufs[i].buf_count)
  1055. ++count;
  1056. }
  1057. DRM_DEBUG("count = %d\n", count);
  1058. if (request->count >= count) {
  1059. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1060. if (dma->bufs[i].buf_count) {
  1061. struct drm_buf_desc __user *to =
  1062. &request->list[count];
  1063. struct drm_buf_entry *from = &dma->bufs[i];
  1064. if (copy_to_user(&to->count,
  1065. &from->buf_count,
  1066. sizeof(from->buf_count)) ||
  1067. copy_to_user(&to->size,
  1068. &from->buf_size,
  1069. sizeof(from->buf_size)) ||
  1070. copy_to_user(&to->low_mark,
  1071. &from->low_mark,
  1072. sizeof(from->low_mark)) ||
  1073. copy_to_user(&to->high_mark,
  1074. &from->high_mark,
  1075. sizeof(from->high_mark)))
  1076. return -EFAULT;
  1077. DRM_DEBUG("%d %d %d %d %d\n",
  1078. i,
  1079. dma->bufs[i].buf_count,
  1080. dma->bufs[i].buf_size,
  1081. dma->bufs[i].low_mark,
  1082. dma->bufs[i].high_mark);
  1083. ++count;
  1084. }
  1085. }
  1086. }
  1087. request->count = count;
  1088. return 0;
  1089. }
  1090. /**
  1091. * Specifies a low and high water mark for buffer allocation
  1092. *
  1093. * \param inode device inode.
  1094. * \param file_priv DRM file private.
  1095. * \param cmd command.
  1096. * \param arg a pointer to a drm_buf_desc structure.
  1097. * \return zero on success or a negative number on failure.
  1098. *
  1099. * Verifies that the size order is bounded between the admissible orders and
  1100. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1101. *
  1102. * \note This ioctl is deprecated and mostly never used.
  1103. */
  1104. int drm_legacy_markbufs(struct drm_device *dev, void *data,
  1105. struct drm_file *file_priv)
  1106. {
  1107. struct drm_device_dma *dma = dev->dma;
  1108. struct drm_buf_desc *request = data;
  1109. int order;
  1110. struct drm_buf_entry *entry;
  1111. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1112. return -EINVAL;
  1113. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1114. return -EINVAL;
  1115. if (!dma)
  1116. return -EINVAL;
  1117. DRM_DEBUG("%d, %d, %d\n",
  1118. request->size, request->low_mark, request->high_mark);
  1119. order = order_base_2(request->size);
  1120. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1121. return -EINVAL;
  1122. entry = &dma->bufs[order];
  1123. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1124. return -EINVAL;
  1125. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1126. return -EINVAL;
  1127. entry->low_mark = request->low_mark;
  1128. entry->high_mark = request->high_mark;
  1129. return 0;
  1130. }
  1131. /**
  1132. * Unreserve the buffers in list, previously reserved using drmDMA.
  1133. *
  1134. * \param inode device inode.
  1135. * \param file_priv DRM file private.
  1136. * \param cmd command.
  1137. * \param arg pointer to a drm_buf_free structure.
  1138. * \return zero on success or a negative number on failure.
  1139. *
  1140. * Calls free_buffer() for each used buffer.
  1141. * This function is primarily used for debugging.
  1142. */
  1143. int drm_legacy_freebufs(struct drm_device *dev, void *data,
  1144. struct drm_file *file_priv)
  1145. {
  1146. struct drm_device_dma *dma = dev->dma;
  1147. struct drm_buf_free *request = data;
  1148. int i;
  1149. int idx;
  1150. struct drm_buf *buf;
  1151. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1152. return -EINVAL;
  1153. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1154. return -EINVAL;
  1155. if (!dma)
  1156. return -EINVAL;
  1157. DRM_DEBUG("%d\n", request->count);
  1158. for (i = 0; i < request->count; i++) {
  1159. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1160. return -EFAULT;
  1161. if (idx < 0 || idx >= dma->buf_count) {
  1162. DRM_ERROR("Index %d (of %d max)\n",
  1163. idx, dma->buf_count - 1);
  1164. return -EINVAL;
  1165. }
  1166. idx = array_index_nospec(idx, dma->buf_count);
  1167. buf = dma->buflist[idx];
  1168. if (buf->file_priv != file_priv) {
  1169. DRM_ERROR("Process %d freeing buffer not owned\n",
  1170. task_pid_nr(current));
  1171. return -EINVAL;
  1172. }
  1173. drm_legacy_free_buffer(dev, buf);
  1174. }
  1175. return 0;
  1176. }
  1177. /**
  1178. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1179. *
  1180. * \param inode device inode.
  1181. * \param file_priv DRM file private.
  1182. * \param cmd command.
  1183. * \param arg pointer to a drm_buf_map structure.
  1184. * \return zero on success or a negative number on failure.
  1185. *
  1186. * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
  1187. * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
  1188. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1189. * drm_mmap_dma().
  1190. */
  1191. int drm_legacy_mapbufs(struct drm_device *dev, void *data,
  1192. struct drm_file *file_priv)
  1193. {
  1194. struct drm_device_dma *dma = dev->dma;
  1195. int retcode = 0;
  1196. const int zero = 0;
  1197. unsigned long virtual;
  1198. unsigned long address;
  1199. struct drm_buf_map *request = data;
  1200. int i;
  1201. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1202. return -EINVAL;
  1203. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1204. return -EINVAL;
  1205. if (!dma)
  1206. return -EINVAL;
  1207. spin_lock(&dev->buf_lock);
  1208. if (atomic_read(&dev->buf_alloc)) {
  1209. spin_unlock(&dev->buf_lock);
  1210. return -EBUSY;
  1211. }
  1212. dev->buf_use++; /* Can't allocate more after this call */
  1213. spin_unlock(&dev->buf_lock);
  1214. if (request->count >= dma->buf_count) {
  1215. if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
  1216. || (drm_core_check_feature(dev, DRIVER_SG)
  1217. && (dma->flags & _DRM_DMA_USE_SG))) {
  1218. struct drm_local_map *map = dev->agp_buffer_map;
  1219. unsigned long token = dev->agp_buffer_token;
  1220. if (!map) {
  1221. retcode = -EINVAL;
  1222. goto done;
  1223. }
  1224. virtual = vm_mmap(file_priv->filp, 0, map->size,
  1225. PROT_READ | PROT_WRITE,
  1226. MAP_SHARED,
  1227. token);
  1228. } else {
  1229. virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
  1230. PROT_READ | PROT_WRITE,
  1231. MAP_SHARED, 0);
  1232. }
  1233. if (virtual > -1024UL) {
  1234. /* Real error */
  1235. retcode = (signed long)virtual;
  1236. goto done;
  1237. }
  1238. request->virtual = (void __user *)virtual;
  1239. for (i = 0; i < dma->buf_count; i++) {
  1240. if (copy_to_user(&request->list[i].idx,
  1241. &dma->buflist[i]->idx,
  1242. sizeof(request->list[0].idx))) {
  1243. retcode = -EFAULT;
  1244. goto done;
  1245. }
  1246. if (copy_to_user(&request->list[i].total,
  1247. &dma->buflist[i]->total,
  1248. sizeof(request->list[0].total))) {
  1249. retcode = -EFAULT;
  1250. goto done;
  1251. }
  1252. if (copy_to_user(&request->list[i].used,
  1253. &zero, sizeof(zero))) {
  1254. retcode = -EFAULT;
  1255. goto done;
  1256. }
  1257. address = virtual + dma->buflist[i]->offset; /* *** */
  1258. if (copy_to_user(&request->list[i].address,
  1259. &address, sizeof(address))) {
  1260. retcode = -EFAULT;
  1261. goto done;
  1262. }
  1263. }
  1264. }
  1265. done:
  1266. request->count = dma->buf_count;
  1267. DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
  1268. return retcode;
  1269. }
  1270. int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
  1271. struct drm_file *file_priv)
  1272. {
  1273. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1274. return -EINVAL;
  1275. if (dev->driver->dma_ioctl)
  1276. return dev->driver->dma_ioctl(dev, data, file_priv);
  1277. else
  1278. return -EINVAL;
  1279. }
  1280. struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
  1281. {
  1282. struct drm_map_list *entry;
  1283. list_for_each_entry(entry, &dev->maplist, head) {
  1284. if (entry->map && entry->map->type == _DRM_SHM &&
  1285. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  1286. return entry->map;
  1287. }
  1288. }
  1289. return NULL;
  1290. }
  1291. EXPORT_SYMBOL(drm_legacy_getsarea);